0

So I am trying to find out if string contains standalone 'c++' substring, for example:

'abcabcabc c++ abc abc' should return true

but

'abcabcabc c++11 abc abc' should return false.

I am trying to achieve it using this regex: r'\bc\+\+\b' which works fine for exactly same scenario for 'foo' substring instead of 'c++' (r'\bfoo\b', it returns true for 'abc abc foo abc abc' and false for 'abc abc foo12 abc abc') but it does not work for 'c++'. :(

What am I missing?

I am using python's re module for that, regex101.com yields the same results.

marke
  • 1,024
  • 7
  • 20

1 Answers1

0

You can use a negative lookahead to avoid a match if it's followed by a word character:

\bc\+\+(?!\w)

Demo: https://regex101.com/r/d3VHyH/1

blhsing
  • 91,368
  • 6
  • 71
  • 106