什么导致回溯

要查找匹配项,正则表达式引擎将逐个使用字符。当部分匹配开始时,引擎将记住起始位置,以便在以下字符未完成匹配的情况下返回。

  • 如果匹配完成,则没有回溯
  • 如果匹配未完成,引擎将回溯该字符串(就像回放旧磁带时)以尝试查找整个匹配项。

例如:\d{3}[a-z]{2} 对字符串 abc123def 将被浏览:

abc123def
^ Does not match \d
abc123def
 ^ Does not match \d
abc123def
  ^ Does not match \d
abc123def
   ^ Does match \d (first one)
abc123def
    ^ Does match \d (second one)
abc123def
     ^ Does match \d (third one)
abc123def
      ^ Does match [a-z] (first one)
abc123def
       ^ Does match [a-z] (second one)
           MATCH FOUND

现在让我们用相同的字符串(abc123def)将正则表达式更改为\d{2}[a-z]{2}

abc123def
^ Does not match \d
abc123def
 ^ Does not match \d
abc123def
  ^ Does not match \d
abc123def
   ^ Does match \d (first one)
abc123def
    ^ Does match \d (second one)
abc123def
     ^ Does not match [a-z]
abc123def
    ^ BACKTRACK to catch \d{2} => (23)
abc123def
      ^ Does match [a-z] (first one)
abc123def
       ^ Does match [a-z] (second one)
           MATCH FOUND