$words_to_match =~ s/^\s*|\s*$/\\s*/g; $words_to_match =~ s/\s+/\\s+/g; $output_line =~ /$words_to_match/i; ...