my @nums = ('1203', '1204', '1207'); my $regex = '\b(?:' + join('|', @nums) + ')\b'; #### c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @nums = ('1203', '1204', '1207'); my $regex = '\b(?:' . join('|', @nums) . ')\b'; print qq{'$regex'}; ;; my @matches = 'w 1207 x 1203 y 9999 z' =~ m{ $regex }xmsg; dd \@matches; " '\b(?:1203|1204|1207)\b' [1207, 1203] #### while ( <$file_h> ) { if (my @matches = m/$regex/g) { ++$matched{$_} for @matches; print "$file $_"; } } #### c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @nums = qw(1203 1204 1207 1111); my ($regex) = map qr{ \b (?: $_) \b }xms, join ' | ', @nums; print $regex; ;; my %seen; for ('w 1207 x 1203 y 9999 z', 'w 11203 x 12033 y 112033 z', 'w 1207 x 1203 y 9999 z 1207 zz', ) { print qq{>$_<} if map ++$seen{$_}, m{ $regex }xmsg; } dd \%seen; ;; my $not_seen = join ' ', grep !$seen{$_}, @nums; print 'num(s) not seen: ', $not_seen || '(none)'; " (?msx-i: \b (?: 1203 | 1204 | 1207 | 1111) \b ) >w 1207 x 1203 y 9999 z< >w 1207 x 1203 y 9999 z 1207 zz< { 1203 => 2, 1207 => 3 } num(s) not seen: 1204 1111