0

There is some text like this:

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

I need to find the phrase "Bank of America, N.A." and I use a regular expression with word boundaries:

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

But it doesn't work. Here is my code:

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());
}

The result is:

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

I don't understand why the result of the phrase "Bank of America, N." is true, and false for the phrase "Bank of America, N.A."?

0 Answers0