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