my $pattern = join '|', map "($_)", @patterns; ... if ($line =~ /$pattern/i) { my $matchref = $#-; print "matched $pattern[$matchref - 1]\n"; }