tags:

views:

54

answers:

4

Hi guys,

I've spent some time, but still have to solution. I need regular expression that is able to match a words with signs in it (like c++) in string.

I've used /\bword\b/, for "usual" words, it works OK. But as soon as I try /\bC\+\+\b/ it just does not work. It some how works wrong with a plus signs in it.

I need a regex to detect if input string contains c++ word in it. Input like,

"c++ developer"
"using c++ language" 

etc.

ps. Using C#, .Net Regex.Match function.

Thanks for help!

+1  A: 

+ is a special character so you need to escape it

\bC\+\+(?!\w)

Note that we can't use \b because + is not a word-character.

KennyTM
Hi Kenny, I'm using it. It was not displayed in view. I've corrected a question. Does not work like that.
alexanderb
@alex: Have you tried `(?!\w)`?
KennyTM
with (?!\w) it does exactly what I need! thanks a lot
alexanderb
+1  A: 

Plus sign have special meaning so you will have to escape it with \. The same rule applies to these characters: \, *, +, ?, |, {, [, (,), ^, $,., #, and white space

UPDATE: the problem was with \b sequence

Viktor Stískala
+4  A: 

The problem isn't with the plus character, that you've escaped correctly, but the \b sequence. It indicates a word boundary, which is a point between a word character (alphanumeric) and something else. Plus isn't a word character, so for \b to match, there would need to be a word character directly after the last plus sign.

\bC\+\+\b matches "Test C++Test" but not "Test C++ Test" for example. Try something like \bC\+\+\s if you expect there to be a whitespace after the last plus sign.

calmh
Specifically `\b` matches between a `\w` and a `\W` (or visa versa).
Richard
thank you for great answer!\bC\+\+\s, works great, but it also matches space after last + sign, it is not big issue, but anyway a little unexpected.
alexanderb
@alexander You can make the space a lookahead to avoid including it in the match: `\bC\+\+(?=\s)`.
calmh
+1  A: 

As the others said, your problem isn't the + sign you've escaped correctly but the \b that is a zero-lenght char that match word boundary that takes place between word \w and non-word \W char.

There is also another mistake in your regex, you want to match char C (uppercase) with c++ (lowercase).To do so you have to change your regex to /\bc\+\+/ or use the i modifier to match case insensitive : /\bc\+\+/i

M42