Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 为什么在使用带否定字符集的星号时不进行回溯_Regex_Pcre - Fatal编程技术网

Regex 为什么在使用带否定字符集的星号时不进行回溯

Regex 为什么在使用带否定字符集的星号时不进行回溯,regex,pcre,Regex,Pcre,实际上我理解什么是回溯,即引擎应用贪婪量词时的状态,这会导致其他原子失败,因此引擎开始回溯到前一个状态,以逐渐放弃匹配,以便匹配剩余的原子 但是当我在这个“abcv上使用这个模式时,我得到了意想不到的行为,我写它是为了检查失败时会发生什么。我希望引擎能采取以下步骤: 发动机匹配“ 然后贪婪量化的否定字符集将匹配abcv 发动机无法匹配最后一个“ 因此,它应该回溯到[^”]*,一个接一个地放弃试图匹配剩余原子的字符 但是当我在regex101上测试它时,引擎不会回溯,但是每次失败时它都会从另一

实际上我理解什么是回溯,即引擎应用贪婪量词时的状态,这会导致其他原子失败,因此引擎开始回溯到前一个状态,以逐渐放弃匹配,以便匹配剩余的原子

但是当我在这个
“abcv
上使用这个模式时,我得到了意想不到的行为,我写它是为了检查失败时会发生什么。我希望引擎能采取以下步骤:

  • 发动机匹配
  • 然后贪婪量化的否定字符集将匹配abcv
  • 发动机无法匹配最后一个
  • 因此,它应该回溯到
    [^”]*
    ,一个接一个地放弃试图匹配剩余原子的字符
但是当我在regex101上测试它时,引擎不会回溯,但是每次失败时它都会从另一个位置重新启动。那么我在这里遗漏了什么呢

这正是人们所期望的吗?如果是,有人能解释为什么吗

更新

我需要提到的是,
“*”
回溯,如果你检查引擎步骤,你会发现它开始一个接一个地给出字符,但有问题的一个没有。为什么这两个
*
[^”]*
都是贪婪的量词,匹配相同的文本,但必须回溯,另一个没有。PCRE在这里使用“自动占有”优化,因为它“看到”除了两个
之间的
之外,没有办法匹配任何其他字符。见:

PCRE\u NO\u AUTO\u拥有

  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.
您可以在
“[^”]*”
前面加上
(*NO\u AUTO\u have)
PCRE动词: