$text =~ s/ (?<=\.\x20) (\w+) /foo($1)/xeg; sub foo { my $word = shift; my $lc_word = lc $word; return $word if not exists $hash{ $lc_word }; return $lc_word; }