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!
A regular expression followed by a plus sign ( + ) matches one or more occurrences of the one-character regular expression. If there is any choice, the first matching string in a line is used. A regular expression followed by a question mark ( ? ) matches zero or one occurrence of the one-character regular expression.
The word boundary \b matches positions where one side is a word character (usually a letter, digit or underscore—but see below for variations across engines) and the other side is not a word character (for instance, it may be the beginning of the string or a space character).
The plus ( + ) is a quantifier that matches one or more occurrences of the preceding element. The plus is similar to the asterisk ( * ) in that many occurrences are acceptable, but unlike the asterisk in that at least one occurrence is required.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
+
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With