my $targets_file = '/Users/me/test.txt'; # (I'd rather get this from @ARGV) open( my $urdu_words, '<', $targets_file ) # (2nd arg might need ':utf8' too) or die "$targets_file: $!\n"; my @target_strings = <$urdu_words>; close $urdu_words; chomp @target_strings; my $target_regex = join( '|', @target_strings ); # Now open and read from the html file # Use a hash to kept track of matches, so you can sort them later: my $html_file = '/Users/me/platts_wkg.html'; # could get that from @ARGV too open( my $platts, '<', $html_file ) or die "$html_file: $!\n"; my %matches; while (<$platts>) { if ( /^.*?

.*? ($target_regex) / ) { # note: spaces are now OUTSIDE parens $matches{$1} .= " $_"; } } # At this point, it would be easy to dump all the matches to a file, and # then edit that file manually, if you want: my $matched_file = '/Users/me/matches_found.txt'; open( my $output, '>', $matched_file ) or die "$matched_file: $!\n"; for my $match ( sort keys %matches ) { print $output "Matches found for target: $match\n $matches{$match}\n"; }