use strict; use warnings; my $string = "breakfast break"; if( $string =~ m/fast\s(break)|(break)fast/ ) { print "\$1 contains ", defined( $1 ) ? $1 : "undef", "\n"; print "\$2 contains ", defined( $2 ) ? $2 : "undef", "\n"; print "The portion of the string that matched was $&\n"; } __OUTPUT__ $1 contains undef $2 contains break The portion of the string that matched was breakfast