I have a rather peculiar problem. I’m trying to find a pattern like [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 this should always output true, regardless of what someString is. However:
- if
someStringends with a word character (e.g. “abc”),trueis outputted; - if
someStringends with a word boundary (e.g. “abc.”),falseis outputted.
Any ideas what is happening? My current workaround is to use \W instead of \b, but I’m not sure of the implications.
A dot then a space is not a word boundary.
A word boundary is between a word character, then a non-word character, or visa versa.
ie between
[a-zA-Z0-9_][^a-zA-Z0-9_]or[^a-zA-Z0-9_][a-zA-Z0-9_]