Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';t以“quot;结尾的正则表达式模式&引用;使用单词边界时是否生成匹配?_Java_Regex_Quote - Fatal编程技术网

Java 为什么';t以“quot;结尾的正则表达式模式&引用;使用单词边界时是否生成匹配?

Java 为什么';t以“quot;结尾的正则表达式模式&引用;使用单词边界时是否生成匹配?,java,regex,quote,Java,Regex,Quote,在以下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("Patt

在以下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.”,我确实有一个匹配项


谢谢您的帮助。

之后没有边界单词字符和非单词字符之间出现边界。由于
(空格)都是非单词字符,因此它们之间没有边界


如果在模式中使用“myphase”,则会得到匹配,因为单词字符
e

之间存在边界,它不匹配,因为点(
)被认为不是“单词”字符,所以在文字点之后不会有单词边界(当下一个字符是空格时)


仅供参考,“word”字符(有自己的regex
\w
)相当于字符类
[a-zA-Z0-9
也许您试图使用\s而不是\b?

否。我确实需要“\b”,因为“\s”只匹配空格。谢谢。我现在理解了。谢谢您的解释。