由于负向预测的位置而导致的匹配差异?
我在正则表达式中存在很多混淆,我试图解决它们。 这里我有以下字符串:
{start}do or die{end}extended string
我的两个不同的正则表达式,我只改变了点的位置:
(.(?!{end}))* //returns: {start}do or di
//^ See here
((?!{end}).)* //returns: {start}do or die
//^ See here
为什么第一个正则表达式会吃最后一个“e”?
而且这个负面预测如何使这个量词不贪心? 我的意思是为什么它不能消费{end}之外的字符?
用你的负面看法,你说,这是不可能匹配的正则表达式,你的情况是: {end}
。 和.
捕获除了新行之外的所有内容。
所以你的第一个正则表达式是:
(.(?!{end}))*
它遗漏了e
,因为: e{end}
由于负向预测而不能匹配。 在你的第二个正则表达式中,在另一边有点的时候,它可以直到: {end}d
所以e
被包含在你的第二个正则表达式中。
我已经为正则表达式完成任务计算了正则表达式引擎的工作流程......
首先,对于(.(?!{end}))*
正则表达式引擎的方法如下...
"{start}do or die{end}extended string"
^ .(dot) matches "{" and {end} tries to match here but fails.So "{" included
"{start}do or die{end}extended string"
^ . (dot) matches "s" and {end} tries to match here but fails.So "s" included
....
....so on...
"{start}do or die{end}extended string"
^ (dot) matches "e" and {end} here matches "{end}" so "e" is excluded..
so the match we get is "{start}do or di"
对于secodn正则表达式((?!{end})。)* ....
"{start}do or die{end}extended string"
^ {end} regex tries to match here but fails to match.So dot consumes "{".
"{start}do or die{end}extended string"
^ {end} regex tries to match here but fails again.So dot consumes "s".
....
..so on..
"{start}do or die{end}extended string"
^ {end} regex tries to match here but fails.So dot consumes the "e"
"{start}do or die{end}extended string"
^ {end} regex tries to match here and succeed.So the whole regex fail here.
So we ended up with a match which is "{start}do or die"
链接地址: http://www.djcxy.com/p/27607.html
上一篇: difference in match due to the position of negative lookahead?
下一篇: Building a Swift Framework with Xcode 7 (Beta 3) to use as an Embedded Binary