c9w4bb4t4b
c9w4bb4t4b

Reputation: 13

Named Subpattern backtracking does not work in PCRE

Given the following PCRE regex:

(?(DEFINE)(?'pat'a|ab))^(?P>pat)b$

I expected that it would match the strings ab and abb. However, it only matches ab (and not abb, see https://regex101.com/r/F70wge/1). It seems that the backtracker does not go into the named subpattern.

When inlining the pattern as follows:

^(?:a|ab)b$

Both strings ab and abb are matched as expected.

Is it possible to change the regex above (with named pattern) in order to make both strings match without inlining the pattern?

Upvotes: 1

Views: 50

Answers (1)

melpomene
melpomene

Reputation: 85887

This is a limitation in older versions of PCRE. The only fix I know is upgrading to version 10.30.

http://www.pcre.org/changelog.txt:

Version 10.30 14-August-2017

  1. 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.

(Emphasis mine.)

Upvotes: 2

Related Questions