Java Regex中matches()和find()之间的区别

She*_*ari 234 java regex

我想明白之间的差别matches()find().

根据Javadoc(根据我的理解),matches()即使它找到了它正在寻找的东西,它也会搜索整个字符串,并find()在它找到它所寻找的内容时停止.

如果这个假设是正确matches()find(),除非你想要计算它找到的匹配数,否则我无法看到你想要使用的代替.

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

总结一下:

  1. 我的假设是否正确?
  2. 何时使用matches()而不是find()

San*_*rma 286

matches尝试将表达式与整个字符串进行匹配,^$在模式的开头和结尾隐式添加a ,这意味着它不会查找子字符串.因此,此代码的输出:

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
*/
Run Code Online (Sandbox Code Playgroud)

123是一个子串,a123b所以find()方法输出true.matches()只有'看'与a123b它不相同123,因而输出错误.

  • 这个答案有误导性.`matchers()`不仅仅是一个带有隐含的^和$的`find()`.请注意,如果不在`reset()`之前调用`.find()`可能会有不同的结果,而`matches()`将始终返回相同的结果.请参阅下面的答案. (23认同)

kha*_*hik 76

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

  • 您可以说`matches(p)`与`find("^"+ p +"$")`相同,如果那更清楚的话. (33认同)
  • `Pattern.compile("some pattern").matcher(str).matches()`等于`Pattern.compile("^ some pattern $").matcher(str).find()` (5认同)
  • 只是一个澄清答案的例子:"[az] +"与字符串"123abc123"将失败使用matches()但将使用find()成功. (4认同)
  • @Max恰好,"123abc123".匹配("[az] +")`将失败,就像"123abc123".find("^ [az] + $")`那样.我的观点是,`matches()`用于完全匹配,就像`find()`同时包含起始和结束锚点. (3认同)
  • @AlexR/@jensgram:`...("some pattern").matcher(str).matches()`*不*完全等于`...("^ some pattern $").matcher(str) .find()`这在第一次调用时才是真的.请参阅下面的答案. (2认同)

L. *_*nda 57

matches()只有匹配完整的字符串才会返回true. 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());
Run Code Online (Sandbox Code Playgroud)

将输出:

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

因此,find()如果Matcher未重置对象,则多次调用时要小心,即使正则表达式被包围^$匹配完整字符串也是如此.

  • 很有帮助的队友 (2认同)

Sum*_*ada 5

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
        }
    }
Run Code Online (Sandbox Code Playgroud)