如何找到带点的短语?

时间:2017-03-30 09:00:50

标签: java regex word-boundary

有一些这样的文字:

*text text text Bank of America, N.A., text text text*

我需要找到“美国银行,N.A。”这句话。我使用带有单词边界的正则表达式:

"\\b" + Pattern.quote(phrase) + "\\b"

但它不起作用。 这是我的代码:

String line = "text text text Bank of America, N.A., text text text";
for (String phrase: new String[] {"Bank of America", "Bank of America, N.", "Bank of America, N.A", "Bank of America, N.A.", "Bank of America, N.A.,", "Bank of America, N.A., "}){
   Pattern p = Pattern.compile("\\b" + Pattern.quote(phrase) + "\\b");
   System.out.println(phrase + " = " + p.matcher(line).find());
}

结果是:

Bank of America = true
Bank of America, N. = true
Bank of America, N.A = true
Bank of America, N.A. = false
Bank of America, N.A., = false
Bank of America, N.A.,  = true

我不明白为什么“Bank of America,N”这句话的结果。对于“美国银行,N.A。”这句话是真的,是假的吗?

0 个答案:

没有答案