Differences Among Greedy, Reluctant, and Possessive Quantifiers
来源:互联网 发布:java 红黑树原理 编辑:程序博客网 时间:2024/04/30 09:22
Differences Among Greedy, Reluctant, and Possessive Quantifiers
There are subtle differences among greedy, reluctant, and possessive quantifiers.Greedy quantifiers are considered "greedy" because they force the matcher to read in, or eat, the entire input string prior to attempting the first match. If the first match attempt (the entire input string) fails, the matcher backs off the input string by one character and tries again, repeating the process until a match is found or there are no more characters left to back off from. Depending on the quantifier used in the expression, the last thing it will try matching against is 1 or 0 characters.
The reluctant quantifiers, however, take the opposite approach: They start at the beginning of the input string, then reluctantly eat one character at a time looking for a match. The last thing they try is the entire input string.
Finally, the possessive quantifiers always eat the entire input string, trying once (and only once) for a match. Unlike the greedy quantifiers, possessive quantifiers never back off, even if doing so would allow the overall match to succeed.
To illustrate, consider the input string xfooxxxxxxfoo
.
The first example uses the greedy quantifierEnter your regex: .*foo // greedy quantifierEnter input string to search: xfooxxxxxxfooI found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.Enter your regex: .*?foo // reluctant quantifierEnter input string to search: xfooxxxxxxfooI 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 quantifierEnter input string to search: xfooxxxxxxfooNo match found.
.*
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.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.
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.
- Differences Among Greedy, Reluctant, and Possessive Quantifiers
- Greedy quantifiers, Reluctant quantifiers, Possessive quantifiers
- Greedy vs. Reluctant vs. Possessive Quantifiers
- Greedy vs. Reluctant vs. Possessive Quantifiers
- Greedy Reluctant Possessive区别
- Greedy Reluctant Possessive 正则表达式
- greedy、reluctant和possessive量词
- greedy、reluctant和possessive量词的区别
- greedy、reluctant和possessive量词的区别
- 正则表达式:Greedy、Reluctant、Possessive 区别
- Java正则中的Greedy、Reluctant 、Possessive
- Java正则中的Greedy、Reluctant 、Possessive qualifier
- [转载]正则表达式 的greedy、reluctant和possessive量词
- Java正则表达中Greedy Reluctant Possessive 的区别
- 正则表达式Greedy、Reluctant、Possessive三种策略的区别
- java 正则表达式Greedy、Reluctant、Possessive的理解
- Java正则表达中Greedy Reluctant Possessive 的区别
- 正则表达式中Greedy、Reluctant、Possessive数量词的区别
- FTP命令详解
- oracle分页查询
- PHP Session丢失问题解决
- ORACLE 中ROWNUM用法总结!(精华)
- 未来
- Differences Among Greedy, Reluctant, and Possessive Quantifiers
- 常用电源IC
- static方法(与隐式static方法)
- EXISTS、IN、NOT EXISTS、NOT IN的区别
- 新年新打算
- (转)volatile的作用
- 常用电源IC
- Java代码执行顺序-样例
- 批处理set命令注意事项