in reply to better way to get last named capture group

For your example, wouldn't a branch reset be more appropriate?
  • Comment on Re: better way to get last named capture group

Replies are listed 'Best First'.
Re^2: better way to get last named capture group
by ysth (Canon) on Jul 01, 2025 at 20:21 UTC
    Exactly the opposite! I want to distinguish which branch matched, when the matched strings could all be the same.

      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.