foreach my $word (split /\s+/, $line) { $word =~ s/microsoft/monopoly/gi; print $word if $word =~ /yes/; }