I would like to do a zero padded integer with regex substitution. I have for example the following string:
my $string = '3-7+9-15';
and want for output:
03-07+09-15
i know i could use sprintf, but i must first retrieve each integer do the sprintf and then re-join the integers into a string :(
i would like to do it in one pass with a regex substitution. is it possible ?
I would choose to use sprintf
in combination with a regex substitution
use strict;
use warnings 'all';
use feature 'say';
my $s = '3-7+9-15';
$s =~ s{(\d+)}{ sprintf '%02d', $1 }ge;
say $s;
03-07+09-15
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With