'Regex to detect string including special characters

I have these inputs:

s1 = 'I am using c++ programming'. 

s2 = = 'I am usingc++ programming'. 

I want to check if s1 or s2 contains the exact word c++.

running this regex on both s1 and s2, it does not give any output:

x=s1 #x=s2

if re.search(r'\bc\+\+\b', x):
    print(x)

expected result: detect c++ in s1



Solution 1:[1]

You could do the following

import re
x = 'I am using c++ programming'

pattern = re.compile(" c\+\+ ")
results = re.findall(pattern, x)

if results:
    print(x)

If c++ is not in the string, results will be empty.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1