命名子模式回溯在 PCRE 中不起作用
Named Subpattern backtracking does not work in PCRE
给定以下 PCRE 正则表达式:
(?(DEFINE)(?'pat'a|ab))^(?P>pat)b$
我预计它会匹配字符串 ab
和 abb
。但是,它只匹配 ab
(而不匹配 abb
,请参阅 https://regex101.com/r/F70wge/1)。回溯器似乎没有进入命名的子模式。
按如下方式内联模式时:
^(?:a|ab)b$
字符串 ab
和 abb
均符合预期。
是否可以更改上面的正则表达式(使用命名模式)以使两个字符串匹配而不内联模式?
这是旧版 PCRE 的限制。我知道的唯一修复方法是升级到版本 10.30。
http://www.pcre.org/changelog.txt:
Version 10.30 14-August-2017
- The main interpreter, pcre2_match(), has been refactored into a new version
that does not use recursive function calls (and therefore the stack) for
remembering backtracking positions. This makes --disable-stack-for-recursion a
NOOP. The new implementation allows backtracking into recursive group calls in
patterns, making it more compatible with Perl, and also fixes some other
hard-to-do issues such as #1887 in Bugzilla.
(强调我的。)
给定以下 PCRE 正则表达式:
(?(DEFINE)(?'pat'a|ab))^(?P>pat)b$
我预计它会匹配字符串 ab
和 abb
。但是,它只匹配 ab
(而不匹配 abb
,请参阅 https://regex101.com/r/F70wge/1)。回溯器似乎没有进入命名的子模式。
按如下方式内联模式时:
^(?:a|ab)b$
字符串 ab
和 abb
均符合预期。
是否可以更改上面的正则表达式(使用命名模式)以使两个字符串匹配而不内联模式?
这是旧版 PCRE 的限制。我知道的唯一修复方法是升级到版本 10.30。
http://www.pcre.org/changelog.txt:
Version 10.30 14-August-2017
- The main interpreter, pcre2_match(), has been refactored into a new version that does not use recursive function calls (and therefore the stack) for remembering backtracking positions. This makes --disable-stack-for-recursion a NOOP. The new implementation allows backtracking into recursive group calls in patterns, making it more compatible with Perl, and also fixes some other hard-to-do issues such as #1887 in Bugzilla.
(强调我的。)