在以下 Java 代码中:
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!");
}
}
我得到这个输出:
Pattern: \b\Qmyphrase.\E\b
Not found!
拜托,有人能解释一下为什么上面的模式不匹配吗?如果我使用“myphrase”而不是“myphrase”,我确实有一个匹配项。在模式中。
谢谢您的帮助。