Regex looks for a string containing + or # using Java

I am doing a string search using the Java Pattern class. I am trying to match a string (txt) containing "C ++" or "C #" inside using the java Pattern class.

String txt="c++ / c# developer"; Pattern p = Pattern.compile(".*\\b(c\\+\\+|c#)\\b.*" , Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(txt); while (m.find()) { ... break; } 

m.find is always false. What am I doing wrong? thanks Ofer

+4
source share
2 answers

\\b is the word boundary. This means that it coincides between a word and a symbol other than a word. + and # are non-character characters, so you need c++ or c# followed by a letter, number, or underscore. Try deleting \\b or replacing it with \\b (which will require another character without a word after + or # ).

Note that when you use find , you also don't need it .* . find will happily return partial matches. Your template will give you the last appearance of either c++ or c# in the first capture group. If this is not what you want, remove the parentheses and wildcards.

Working demo

EDIT: If you add other alternatives that do end with word characters (like java ). The purest solution is not to use \\b or \\b at all, but create your own boundary condition using a negative result. So you just say "match if there is no next character of the word":

 \\b(c\\+\\+|c#|java)(?!\\w) 

Working demonstration.

+6
source

You can try using ^.*c(\+{2}|\#).*$ . He says find c followed by either 2 + or # . You can see an example here .

0
source

All Articles