为什么Java正则表达式“匹配”与“查找”在使用非贪婪模式时获得不同的匹配?

时间:2014-07-10 16:16:23

标签: java regex

所以我遇到了一个错误,因为期望match()方法找到与使用find()完全相同的匹配。通常情况就是如此,但看起来如果非贪婪的模式可以被拉伸以贪婪地接受整个字符串,那么它是允许的。这似乎是Java中的一个错误。我错了吗?我没有在文档中看到任何表明这种行为的内容。

Pattern stringPattern = Pattern.compile("'.*?'");
String nonSingleString = "'START'===stageType?'active':''";
Matcher m1 = stringPattern.matcher(nonSingleString);
boolean matchesCompleteString = m1.matches();
System.out.println("Matches complete string? " + matchesCompleteString);
System.out.println("What was the match? " + m1.group()); //group() gets the string that matched

Matcher m2 = stringPattern.matcher(nonSingleString);
boolean foundMatch = m2.find(); //this looks for the next match
System.out.println("Found a match in at least part of the string? " + foundMatch);
System.out.println("What was the match? " + m2.group());

输出

  

匹配完整的字符串?真正
  比赛是什么? 'START' === stageType '主动': ''
  在至少部分字符串中找到匹配项?真正
  比赛是什么? 'START'

2 个答案:

答案 0 :(得分:9)

这很有道理。

matches(...)方法必须尝试使用​​整个字符串,因此即使使用非贪婪模式也是如此。

find(...)方法可能会找到一个子字符串,因此如果找到任何匹配的子字符串,它会在该点停止。

答案 1 :(得分:8)

他们应该是不同的。 Matcher#matches尝试使用正则表达式^$围绕正则表达式匹配完整的输入字符串,而Matcher#find匹配正则表达式可以匹配的任何内容。

根据Javadoc

  

public boolean matches()

  试图匹配整个地区   反对模式。如果匹配成功,则可以获得更多信息   通过开始,结束和组方法获得。

  

public boolean find()

  尝试查找与模式匹配的输入序列的下一个子序列。