In the following Java code:
public static void main(String[] args) {
String largeText = "abc myphrase. def";
String phrase = "myphrase.";
Pattern myPattern = Pattern.compile("\\b"+Pattern.quote(phrase)+"\\b");
System.out.println("Pattern: "+myPattern);
Matcher myMatcher = myPattern.matcher( largeText );
boolean found = false;
while(myMatcher.find()) {
System.out.println("Found: "+myMatcher.group());
found = true;
}
if(!found){
System.out.println("Not found!");
}
}
I get this output:
Pattern: \b\Qmyphrase.\E\b
Not found!
Please, can someone explain me why the above pattern does not produce a match? I do have a match if I use “myphrase” instead of “myphrase.” in the pattern.
Thank you for your help.
There is no boundary after the
.A boundary occurs between a word character and a non-word character. Since both.and" "(space) are non-word characters there is no boundary between them.If you use “myphase” in your pattern you get a match because there is a boundary between the word character
eand the..