my $re = join '|', map "\Q$_\E", sort {length $b <=> length $a} keys %dictionary; while () { s/($re)/$dictionary{$1}/g }