my $re1 = qr/beer=(\d{2}).*chips=(\d{3})/; my $re2 = qr/vodka=(\d{2})/; if ( $str =~ /$re1 ) { my $m1 = $1; my $m2 = $2; if ( $str =~ /$re2/ ) { #use that capture and go on... } }