作者:傻傻的笑没心没肺wy | 来源:互联网 | 2023-09-18 05:19
This question already has an answer here:
这个问题在这里已有答案:
- Regex plus vs star difference? 9 answers
正则表达加vs明星差异? 9个答案
What does these following two regular expression means?
以下两个正则表达式意味着什么?
.*? and .+?
Actually I understood usage these Quantifiers i.e.
其实我理解使用这些量词,即
'.' -> Any character
'*' -> 0 or more times
'+' -> once or more times
'?' -> 0 or 1
Indeed, I am literally confused!!! about using .*? and .+?
.Could anybody show up with proper examples for these cases.
的确,我真的很困惑!关于使用。*?和。+?。任何人都可以出现这些案例的适当例子。
And you'r most welcome to share good links that presents useful examples practices. Thanks in advance.
并且非常欢迎您分享介绍有用示例实践的良好链接。提前致谢。
3 个解决方案
2
To illustrate, consider the input string xfooxxxxxxfoo.
为了说明,请考虑输入字符串xfooxxxxxxfoo。
Enter your regex: .*foo // greedy quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.
Enter your regex: .*?foo // reluctant quantifier
Enter input string to search: xfooxxxxxxfoo
I found the text "xfoo" starting at index 0 and ending at index 4.
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.
Enter your regex: .*+foo // possessive quantifier
Enter input string to search: xfooxxxxxxfoo
No match found.
The first example uses the greedy quantifier .* to find "anything", zero or more times, followed by the letters "f" "o" "o". Because the quantifier is greedy, the .* portion of the expression first eats the entire input string. At this point, the overall expression cannot succeed, because the last three letters ("f" "o" "o") have already been consumed. So the matcher slowly backs off one letter at a time until the rightmost occurrence of "foo" has been regurgitated, at which point the match succeeds and the search ends.
第一个例子使用贪心量词。*来找到“任何”,零次或多次,然后是字母“f”“o”“o”。因为量词是贪婪的,所以表达式的。*部分首先会占用整个输入字符串。此时,整体表达式不能成功,因为已经消耗了最后三个字母(“f”“o”“o”)。因此,匹配器一次缓慢地退回一个字母,直到最右边的“foo”被反刍,此时匹配成功并且搜索结束。
The second example, however, is reluctant, so it starts by first consuming "nothing". Because "foo" doesn't appear at the beginning of the string, it's forced to swallow the first letter (an "x"), which triggers the first match at 0 and 4. Our test harness continues the process until the input string is exhausted. It finds another match at 4 and 13.
然而,第二个例子是不情愿的,所以它首先消耗“没有”。因为“foo”没有出现在字符串的开头,所以它被强制吞下第一个字母(“x”),这会在0和4处触发第一个匹配。我们的测试工具继续进程直到输入字符串为累。它在4和13找到另一场比赛。
The third example fails to find a match because the quantifier is possessive. In this case, the entire input string is consumed by .*+, leaving nothing left over to satisfy the "foo" at the end of the expression. Use a possessive quantifier for situations where you want to seize all of something without ever backing off; it will outperform the equivalent greedy quantifier in cases where the match is not immediately found.
第三个例子找不到匹配,因为量词是占有性的。在这种情况下,整个输入字符串被。* +消耗,不留下任何东西以满足表达式末尾的“foo”。使用占有量词来表示你想要抓住所有东西而不会退缩的情况;在没有立即找到匹配的情况下,它将胜过等效的贪心量词。
You can find this in the link http://docs.oracle.com/javase/tutorial/essential/regex/quant.html
您可以在http://docs.oracle.com/javase/tutorial/essential/regex/quant.html链接中找到它。