实际上我理解什么是回溯,这是引擎应用贪婪量词时的状态,导致其他原子失效,因此引擎开始回溯到先前的状态,以便在匹配剩余原子时逐渐放弃匹配。
但是当我在这个"[^"]*"
上使用这种模式"abcv
时,我得到了意想不到的行为,我写它来检查失败时会发生什么。我希望引擎采取这些步骤:
"
"
[^"]*
,逐个放弃角色试图匹配剩余的原子。但是当我在regex101上测试它时,引擎不会回溯,但每次失败时它都会从另一个位置开始。我在这里失踪了什么?
这到底是什么原因?如果是的话,有人会解释原因吗?
更新
我需要提一下".*"
回溯,如果你检查引擎步骤,你会发现它开始逐个给出字符,但有问题的那个没有。为什么这个差异,而.*
和[^"]*
都是贪婪的量词,匹配相同的文本,但一个必须回溯,另一个没有。
PCRE正在使用“自动拥有”优化,因为它“看到”没有办法匹配除两个"
之间的"
之外的任何其他字符。见PCRE docs:
PCRE_NO_AUTO_POSSESS
If this option is set, it disables "auto-possessification". This is an optimization that, for example, turns a+b into a++b in order to avoid backtracks into a+ that can never be successful. However, if callouts are in use, auto-possessification means that some of them are never taken. You can set this option if you want the matching functions to do a full unoptimized search and run all the callouts, but it is mainly provided for testing purposes.
你可以通过在easily check和Qazxswpoi PCRE动词前面加上"[^"]*"
:
(*NO_AUTO_POSSESS)