my @words = qw(foo bar baz); my $regex = join(")|(",grep { quotemeta($_) } @words); if ($input =~ m/($regex)/ ){ my $index = $#- -1; print "matched word $words[$index]\n"; }