为什么Java正则表达式“匹配” vs“找到”使用非贪婪模式时获得不同的匹配? [英] Why does Java regex "matches" vs "find" get a different match when using non-greedy pattern?
问题描述
所以我遇到了一个错误,因为期望match()方法找到与使用find()完全相同的匹配。通常情况就是如此,但看起来如果非贪婪的模式可以被拉伸以贪婪地接受整个字符串,那么它是允许的。这似乎是Java中的一个错误。我错了吗?我没有在文档中看到任何表明此行为的内容。
So I ran into a bug caused by expecting the matches() method to find exactly the same match as using find(). Normally this is the case, but it appears that if a non-greedy pattern can be stretched to greedily accept the whole string, its allowed. This seems like a bug in Java. Am I wrong? I don't see anything in the docs which indicates this behavior.
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());
输出
匹配完整的字符串? true
比赛是什么? 'START'=== stageType?'active':''
在字符串的至少一部分找到匹配项? true
比赛是什么? 'START'
Matches complete string? true
What was the match? 'START'===stageType?'active':''
Found a match in at least part of the string? true
What was the match? 'START'
推荐答案
这很有道理。
匹配(...)
方法必须尝试使用整个字符串,所以即使使用非贪婪模式也是如此。
The matches(...)
method must attempt to consume the whole string, so it does, even with a non-greedy pattern.
find(...)
方法可能会找到一个子字符串,因此如果找到任何匹配的子字符串,它会在该点处停止。
The find(...)
method may find a substring, so it stops at the point if finds any matching substring.
这篇关于为什么Java正则表达式“匹配” vs“找到”使用非贪婪模式时获得不同的匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!