... my @rxs = ( qr/\s*(\w)\s+(\w)\s*$/, qr/\s*(\d)\s+(\d)\s*$/, qr/\s*([a-z])\s+([a-z])\s+([a-z])\s*$/, ); my @result; for my $rx (@rxs) { if ($text =~ /$rx/mgc) { my $match = $&; # match again to extract submatches my @grps = $match =~ $rx; push @result, [ @grps ]; } }