my $text = <## $VAR1 = [ [ 'A', 'B' ], [ '1', '2' ], [ 'a', 'b', 'c' ] ]; #### ... 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 ]; } }