my %lookup = ( 5 => 4, 3 => 2, ); my $t = "I have 5 foo and 4 bar."; if ( $t =~ m{(\d+) foo and $lookup{$1} bar} ) { print "matched.\n"; }