\ b does not match if the previous character is a word boundary

I have a rather strange problem. I am trying to find a template similar [some string][word boundary]. Simplified, my code is:

final Pattern pattern = Pattern.compile(Pattern.quote(someString) + "\\b");
final String value = someString + " ";
System.out.println(pattern.matcher(value).find());

My logic tells me that this should always infer true, no matter what someString. However:

  • If it someStringends with a word character (for example, "abc"), it is output true;
  • If it someStringends with a word boundary (for example, "abc."), Is output false.

Any ideas what is going on? My current workaround is to use \Winstead \b, but I'm not sure about the consequences.

+5
source share
2 answers

Point, then space is not the boundary of words.

, .
.. [a-zA-Z0-9_][^a-zA-Z0-9_] [^a-zA-Z0-9_][a-zA-Z0-9_]

+6

- , . , (2 ), .

\W , ( , \b, , ), .

+4

All Articles