Java Regex中match()和find()的区别

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

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

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

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

总结一下:

  1. 我的假设正确吗?
  2. 什么时候使用matches()而不是find()是有用的?
239870 次浏览

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
*/

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

  • matches() -如果匹配了完整的字符串,将只返回真正的
  • find() -将尝试在 匹配正则表达式的子字符串。

注意强调“__abc4”;在find()情况下。这意味着,多次调用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()时要小心,即使正则表达式被^$包围以匹配完整字符串。

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

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

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


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


3:int count = 0;


4:while(matcher.find()){


5:count++: }

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

find()将根据正则表达式考虑子字符串,而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
}
}