in reply to Re: better way to get last named capture group
in thread better way to get last named capture group

Exactly the opposite! I want to distinguish which branch matched, when the matched strings could all be the same.
  • Comment on Re^2: better way to get last named capture group

Replies are listed 'Best First'.
Re^3: better way to get last named capture group
by ikegami (Patriarch) on Jul 01, 2025 at 22:02 UTC

    With numbered captures, defined( $1 ) tells us if a capture is part of the matching patch.

    The equivalent with named captures would be exists( $+{ name } ).

    To check which of a set, ( grep exists( $+{ $_ } ), qw( name1 name2 name3 ) )[ 0 ] could be used.

    And of course, that simplifies to ( keys( %+ ) )[ 0 ] if only one named capture could have captured.