Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x=s1 #x=s2

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

expected result: detect c++ in s1

>Solution :

You may use this regex for this using different flavors of word boundaries:

\bc\+\+\B

RegEx Demo

RegEx Details:

  • \b: Word boundary between a non-word and word character
  • c\+\+: Match c++
  • \B: Inverse of word boundary to match where \b doesn’t match
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading