为什么Java正则表达式&"matches&"与“查找"使用非贪婪模式时得到不同的匹配? [英] Why does Java regex "matches" vs "find" get a different match when using non-greedy pattern?

查看:43
本文介绍了为什么Java正则表达式&"matches&"与“查找"使用非贪婪模式时得到不同的匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一个错误,该错误是由于期望matchs()方法找到与使用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());

输出

匹配完整的字符串?真实
比赛是什么?'START'=== stageType?'active':''
在至少部分字符串中找到匹配项?真实
比赛是什么?"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'

推荐答案

这很合理.

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

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正则表达式&"matches&"与“查找"使用非贪婪模式时得到不同的匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆