http://www.perlmonks.org?node_id=11155515


in reply to Precise backreference semantics in Perl regular expressions

The main question is whether \2 should have a value at all when that alternate failed to match last time through the alternation.

See the recent bug report Perl #21563. There is definite inconsistency in how backreferences are preserved or discarded when backtracking, but it is unclear what consistent behaviour we should be aiming at.

Based on the expectation I (hvds) described in that issue ("... that we would retain only results from the last successful match, so that ((a)|(b))+ would never return captures of both a and b") with which demerphq agreed, neither of your strings should match - after we match the /a*b?d/ alternate, \2 from the other alternate should be unset.

I hope that at some point we will settle on the principle of how it should behave, and document that clearly; I don't think we're there yet. I would not, however, expect to see extensive comparisons with other regexp engines in Perl's documentation: there are too many moving targets involved. I think I remember that PCRE documentation had more of such comparisons though.