my %bad_words = map { $_ => 1 } qw( bob fred bill ); s/ (.*ing) (.*) (?(?{ $bad_words{$2} })\A(?!\A)) (more) /\1HIT\3/x;