use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 0; my $string_three = 'foo bar [21a] plus (b23) baz bax'; my @string_three_results = $string_three =~ / (?| (\d+) (a|b) | (a|b) (\d+) ) /gx; print 'RESULTS: ', Dumper(\@string_three_results), $/;