$res = $str =~ s{ \d+ }{ $& =~ tr//3/cr }xer; #### $res = $str =~ s{ \d+ }{ 3 x length $& }xer;