http://www.perlmonks.org?node_id=942339


in reply to help required for regexp

I may be reading too much into the OP, but if upaksh wants to translate digit groups in strings like 'f3333_4444_2222' only when the prefix character is an 'f', the other solutions fail (as shown below). Here are a couple of approaches:

>perl -wMstrict -le "my %Hidlabels = ('3333' => '1', '4444' => '2', '2222' => '3'); my $t = 'f3333_4444_2222 g4444_2222_3333 f4444_2222_3333 f3333 _3333'; ;; my $s = $t; $s =~ s{ (\d+) }{$Hidlabels{$1}}xmsg; print qq{'$s'}; ;; $s = $t; $s =~ s{ (?: \G (?<! \A) _ | f) \K (\d+) }{$Hidlabels{$1}}xmsg; print qq{'$s'}; ;; $s = $t; my $f = qr{ f \d{4} (?: _ \d{4}){2} }xms; $s =~ s{ ($f) } { (my $x = $1) =~ s{(\d+)}{$Hidlabels{$1}}xmsg; $x; }xmsge; print qq{'$s'}; " 'f1_2_3 g2_3_1 f2_3_1 f1 _1' 'f1_2_3 g4444_2222_3333 f2_3_1 f1 _3333' 'f1_2_3 g4444_2222_3333 f2_3_1 f3333 _3333'