use warnings FATAL => 'all'; use strict; my %hash = ( 'nc7-52' => 'C_fraterna', 'mi9-87' => 'D_whatever', ); my ($search) = map qr{ $_ }xms, join ' | ', keys %hash ; print "search regex: \n", $search, "\n\n"; # FOR DEBUG print "output: \n"; while () { my $line = $_; $line =~ s{ \A ($search) (?= :) }{$hash{$1}_$1}xms; print $line; } __DATA__ nc7-52:p39|read1|1|19|296|0.0642 ia1-23:p42|read1|1|19|296|0.0642 mi9-87:p77|read1|1|19|296|0.0642