use strict; my $string = "testx_testy_testz"; my $in = "(testx)_(testy)_(testz)"; my $out = '2_1::0'; test("$string","$in","$out"); sub test { my $sub_string = shift; my $sub_in = shift; my $sub_out = shift; print "$sub_string <=> $sub_in <=> $sub_out \n"; my @info = $sub_string =~ /$sub_in/; my $new_string = ""; for my $rr ( 0 .. length($sub_out)-1 ) { my $ch = substr($sub_out,$rr,1); ( $ch =~ /\d/ ) ? $new_string.=$info[$ch] : 1; ( $ch =~ /\D/ ) ? $new_string.=$ch : 1; } print "erg: $new_string \n"; }