正则表达式之贪婪模式讲解
2018-01-17 15:00
204 查看
没有注意过 贪婪模式和 非贪婪模式的含义
查看 这篇文章
参考 https://stackoverflow.com/questions/5319840/greedy-vs-reluctant-vs-possessive-quantifiers
关于正则表达式入门参考正则表达式30分钟入门教程
摘抄
A greedy quantifier first matches as much as possible. So the .* matches the entire string. Then the matcher tries to match the f following, but there are no characters left. So it “backtracks”, making the greedy quantifier match one less thing (leaving the “o” at the end of the string unmatched). That still doesn’t match the f in the regex, so it “backtracks” one more step, making the greedy quantifier match one less thing again (leaving the “oo” at the end of the string unmatched). That still doesn’t match the f in the regex, so it backtracks one more step (leaving the “foo” at the end of the string unmatched). Now, the matcher finally matches the f in the regex, and the o and the next o are matched too. Success!
A reluctant or “non-greedy” quantifier first matches as little as possible. So the .* matches nothing at first, leaving the entire string unmatched. Then the matcher tries to match the f following, but the unmatched portion of the string starts with “x” so that doesn’t work. So the matcher backtracks, making the non-greedy quantifier match one more thing (now it matches the “x”, leaving “fooxxxxxxfoo” unmatched). Then it tries to match the f, which succeeds, and the o and the next o in the regex match too. Success!
In your example, it then starts the process over with the remaining unmatched portion of the string, following the same process.
A possessive (占有)quantifier is just like the greedy quantifier, but it doesn’t backtrack. So it starts out with .* matching the entire string, leaving nothing unmatched. Then there is nothing left for it to match with the f in the regex. Since the possessive quantifier doesn’t backtrack, the match fails there.
+占有量词类似于greedy 模式,但是他不会往回走 .+ 中. 匹配了整个String
图形显示
?非贪婪模式 读取第一个字符 看看批不匹配 .?foo 批不匹配整个 .?foo 如果不匹配那么继续吞下一个字符 直到匹配到第一个能匹配的 然后 在这个基础上 继续匹配下去如果还能匹配就是第二个子串
+ 占有符 .+foo .+ 这个符号现将 整个字符串吞下去然后后面是不是foo 如果不是 结束 没有吐的过程
查看 这篇文章
参考 https://stackoverflow.com/questions/5319840/greedy-vs-reluctant-vs-possessive-quantifiers
关于正则表达式入门参考正则表达式30分钟入门教程
摘抄
A greedy quantifier first matches as much as possible. So the .* matches the entire string. Then the matcher tries to match the f following, but there are no characters left. So it “backtracks”, making the greedy quantifier match one less thing (leaving the “o” at the end of the string unmatched). That still doesn’t match the f in the regex, so it “backtracks” one more step, making the greedy quantifier match one less thing again (leaving the “oo” at the end of the string unmatched). That still doesn’t match the f in the regex, so it backtracks one more step (leaving the “foo” at the end of the string unmatched). Now, the matcher finally matches the f in the regex, and the o and the next o are matched too. Success!
A reluctant or “non-greedy” quantifier first matches as little as possible. So the .* matches nothing at first, leaving the entire string unmatched. Then the matcher tries to match the f following, but the unmatched portion of the string starts with “x” so that doesn’t work. So the matcher backtracks, making the non-greedy quantifier match one more thing (now it matches the “x”, leaving “fooxxxxxxfoo” unmatched). Then it tries to match the f, which succeeds, and the o and the next o in the regex match too. Success!
In your example, it then starts the process over with the remaining unmatched portion of the string, following the same process.
A possessive (占有)quantifier is just like the greedy quantifier, but it doesn’t backtrack. So it starts out with .* matching the entire string, leaving nothing unmatched. Then there is nothing left for it to match with the f in the regex. Since the possessive quantifier doesn’t backtrack, the match fails there.
+占有量词类似于greedy 模式,但是他不会往回走 .+ 中. 匹配了整个String
图形显示
总结:
* 贪婪模式 现将所有的字符串吞下 看是否匹配 如果不匹配 突出一个字符 然后再比叫 以此往复 直到匹配到位置?非贪婪模式 读取第一个字符 看看批不匹配 .?foo 批不匹配整个 .?foo 如果不匹配那么继续吞下一个字符 直到匹配到第一个能匹配的 然后 在这个基础上 继续匹配下去如果还能匹配就是第二个子串
+ 占有符 .+foo .+ 这个符号现将 整个字符串吞下去然后后面是不是foo 如果不是 结束 没有吐的过程
相关文章推荐
- javascript中的正则表达式的贪婪模式和非贪婪模式讲解
- javascript中的正则表达式的贪婪模式和非贪婪模式讲解
- Qt 正则表达式的非贪婪模式
- 正则表达式 贪婪模式和非贪婪模式
- 正则表达式中的贪婪模式与非贪婪模式解析
- 正则表达式非贪婪模式和取反字符类的使用区别
- 正则表达式贪婪与非贪婪模式
- 正则表达式(regex) 贪婪模式、懒惰模式使用方法
- Java 正则表达式 非贪婪模式
- JAVA 正则表达式的三种模式: 贪婪, 勉强和占有的讨论
- Python 正则表达式里面 贪婪模式和非贪婪模式的区别
- 正则表达式, 贪婪模式
- C#正则表达式之贪婪模式
- 正则表达式:表达式的递归匹配及非贪婪模式的效率
- JS正则表达式学习之贪婪和非贪婪模式实例总结
- 正则表达式的贪婪与非贪婪模式
- 正则表达式的“非贪婪模式”
- [疯狂Java]正则表达式:Java正则表达式语法、贪婪模式/勉强模式
- 正则表达式贪婪与非贪婪模式
- javascript中的正则表达式的贪婪模式和非贪婪模式等若干问题