$line =~ s%$matchkey%$dic{$&}%g; # Use the matched string as a key in $dic $line =~ s%($matchkey)%$dic{$1}%g; # Same as above without the performance issues of $&