#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my %hash = ( xxx_text10_yyy => 1, xxx_text11_yyy => 1, mmm_text12_nnn => 1, mmm_text13_nnn => 1, abc_text6_ghi => 1, abc_text7_ghi => 1, abc_text8_ghi => 1, abc_text9_ghi => 1 ); my %xref = ( abc => 'boom', mmm => 'doom', xxx => 'zoom', ); my $re = qr{(?x: ^ ( [^_]+ ) _ ( [^_]+ ) )}; /$re/ and $hash{$_} = join '_', $xref{$1}, $2 for keys %hash; dd \%hash;