#!/usr/bin/perl use strict; use warnings; my %fill = ( 'M(ary)' => sub {"G$1"}, ); while (my $line = ()) { foreach my $key (keys %fill) { if ($line =~ /$key/) { $line =~ s/$key/&{$fill{$key}}/e; } } print "$line"; } __DATA__ Mary has a little lamb __END__ $ ./reghash.pl Gary has a little lamb