我试图理解match()和find()之间的区别。

根据Javadoc,(从我的理解),matches()将搜索整个字符串,即使它找到了它正在寻找的东西,而find()将在它找到它正在寻找的东西时停止。

如果这个假设是正确的,我看不出您什么时候想要使用matches()而不是find(),除非您想计算它找到的匹配项的数量。

在我看来,String类应该有find()而不是match()作为内置方法。

总结一下:

我的假设正确吗? 什么时候使用matches()而不是find()有用?


如果整个字符串匹配给定的模式,则Matches返回true。Find尝试查找与模式匹配的子字符串。

Matches尝试将表达式与整个字符串匹配,并在模式的开头隐式地添加^,在模式的末尾隐式地添加$,这意味着它不会查找子字符串。因此这段代码的输出是:

public static void main(String[] args) throws ParseException {
    Pattern p = Pattern.compile("\\d\\d\\d");
    Matcher m = p.matcher("a123b");
    System.out.println(m.find());
    System.out.println(m.matches());

    p = Pattern.compile("^\\d\\d\\d$");
    m = p.matcher("123");
    System.out.println(m.find());
    System.out.println(m.matches());
}

/* output:
true
false
true
true
*/

123是a123b的子字符串,因此find()方法输出true。Matches()只“看到”与123不相同的a123b,因此输出false。

Matches() -仅在匹配完整字符串时才返回true Find()—将尝试在 匹配正则表达式的子字符串。

注意find()时强调的是“next”。这意味着,多次调用find()的结果可能不相同。此外,通过使用find(),可以调用start()来返回子字符串被匹配的位置。


例子

final Matcher subMatcher = Pattern.compile("\\d+").matcher("skrf35kesruytfkwu4ty7sdfs");
System.out.println("Found: " + subMatcher.matches());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find());
System.out.println("Found: " + subMatcher.find());
System.out.println("Matched: " + subMatcher.matches());

System.out.println("-----------");
final Matcher fullMatcher = Pattern.compile("^\\w+$").matcher("skrf35kesruytfkwu4ty7sdfs");
System.out.println("Found: " + fullMatcher.find() + " - position " + fullMatcher.start());
System.out.println("Found: " + fullMatcher.find());
System.out.println("Found: " + fullMatcher.find());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());

输出

Found: false
Found: true - position 4
Found: true - position 17
Found: true - position 20
Found: false
Found: false
Matched: false
-----------
Found: true - position 0
Found: false
Found: false
Matched: true
Matched: true
Matched: true
Matched: true

因此,如果Matcher对象没有重置,那么在多次调用find()时要小心,即使正则表达式被^和$包围以匹配完整的字符串。

匹配();不缓冲,但find()缓冲。Find()首先搜索到字符串的末尾,对结果进行索引,并返回布尔值和相应的索引。

这就是为什么当你有一个代码

1:Pattern.compile("[a-z]");

2:Pattern.matcher("0a1b1c3d4");

3:int count = 0;

4:while(matcher.find()){

5:count++: }

第4步:使用模式结构的正则表达式引擎将读取整个代码(由正则表达式[单个字符]指定的索引到索引,以找到至少一个匹配项。如果找到这样的匹配,它将被索引,然后循环将基于索引的结果执行,否则,如果它没有提前计算,如which matches();没有。while语句永远不会执行,因为匹配字符串的第一个字符不是字母。

Find()将根据正则表达式考虑子字符串,而as matches()将考虑完整表达式。

Find()只在表达式的子字符串与模式匹配时才返回true。

public static void main(String[] args) {
        Pattern p = Pattern.compile("\\d");
        String candidate = "Java123";
        Matcher m = p.matcher(candidate);

        if (m != null){
            System.out.println(m.find());//true
            System.out.println(m.matches());//false
        }
    }