4

我正在使用 Java Pattern 类进行一些字符串搜索。我正在尝试使用 java Pattern 类匹配包含“c++”或“c#”的字符串(txt)。

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 总是错误的我做错了什么?谢谢奥弗

4

2 回答 2

6

\\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.

于 2013-04-17T19:30:42.173 回答
0

您可以尝试使用^.*c(\+{2}|\#).*$. 它说 find ac后跟 2+或 a #你可以在这里看到一个例子。

于 2013-04-17T19:28:38.793 回答