if ( $str =~ / (?: (?<_1> BA[ABC]{2} ) | (?<_2> CA[CD]{2} ) | (?<_3> DA[SC]{2} ) ) /x ) { my $first = substr( ( keys( %+ ) )[0], 1 ); ... }