use strict; use warnings; use 5.010; my $string = 'a6a'; my $new_val = 9; $string =~ s/(\d+)/ $1 + $new_val /e; say $string; --output:-- a15a $string =~ s/(\d+)/ "hello$1world" /e; say $string; --output:-- ahello15worlda $string = 'aXXXa'; $string =~ s/(XXX)/ my $x = $1; #$1 is read only, so can't use s/// on it $x =~ s{X}{Y}g; #use a unique delimiter so no conflict $x #previous line returns # of substitutions /e; say $string; --output:-- aYYYa