in reply to Re^2: Leaking Regex Captures
in thread Leaking Regex Captures

'2c3w' cannot be matched only by the second parentheses; the first parentheses must match as well, otherwise the entire match would fail.

Incorrect.   You are using alternation so only one of the alternatives has to match for the entire match to be successful.

Given that both the first and the second must have matched successfully,

Using alternation only one or the other can match successfully, but not both at the same time.

Update:

PS: How does one tell which capture matched, if there is garbage in the other capture variables?

From perlvar:

One can use "$#-" to find the last matched subgroup in the last successful match.

Replies are listed 'Best First'.
A tale of two regex
by SuicideJunkie (Vicar) on Aug 05, 2009 at 17:32 UTC

    Perhaps a stepwise commented example would make it clear what my issue is.

    $1 should DEFINITELY not be 'c'!
    Where did the 'a' go?


    Compare with: And this time, $1 was handled sensibly.
Re^4: Leaking Regex Captures
by SuicideJunkie (Vicar) on Aug 04, 2009 at 18:13 UTC
    '2c3w' cannot be matched only by the second parentheses; the first parentheses must match as well, otherwise the entire match would fail.
    Incorrect. You are using alternation so only one of the alternatives has to match for the entire match to be successful.

    The alternation is repeated with the + so that multiple branches can match. And the regex is anchored with an '^' so in order for the '3w' to match, the '2c' must match first. Not at the same time, but they both do match on the same string.

    Adding a '$' anchor does not change the symptoms, and was left out of the example.