\\b
is a word boundary. Which means it matches between a word and a non-word character. +
and #
are both non-word characters, so you require c++
or c#
to be followed by a letter, digit or underscore. Try removing the \\b
or replacing it with a \\B
(which would require that there is another non-word character after the +
or #
).
Note that, when you are using find
, you don't need the .*
either. find
will happily return partial matches. Your pattern would give you the last occurrence of either c++
or c#
in the first capturing group. If that is not what you want, remove the parentheses and wildcards.
Working demo.
EDIT: If you are adding other alternatives that do end in word characters (like java
). The cleanest solution would be not to use \\b
or \\B
at all, but create your own boundary condition using a negative lookahead. This way you are simply saying "match if there is no word character next":
\\b(c\\+\\+|c#|java)(?!\\w)
Working demo.