my @strings_to_match = ('192.168.200.', '10.10.200'); my ($regex) = map { qr/$_/ } join '|', map {quotemeta} sort { length $b <=> length $a } @strings_to_match; my @filtered_result; while (<>) { push @filtered_result, $_ if m{ ,SEVERE, .* $regex }x; }