my $str1 = "beer=10&otherstuff&vodka=20&otherstuff&chips=100"; my $str2 = "beer=10&otherstuff&juice=20&otherstuff&chips=100"; my $re = qr/beer=(\d{2}).*(vodka=(\d{2})).*chips=(\d{3})/; { $str1 =~ /$re/ ? say "matches" : say "doesn't match"; say "$1 $2 $3 $4 "; } { $str2 =~ /$re/? say "matches" : say "doesn't match"; say "$1 $2 $3 $4 "; }