in reply to Regex to replace consecutive tokens
Use look-arounds, that way your regex does not consume the next comma.
$ perl -E ' $str = q{1,2,3,,5,6,,,9,10,,,,14,15,,,,,,,,,,,,,}; $str =~ s{(?<=,)(?=,)}{0}g; say $str;' 1,2,3,0,5,6,0,0,9,10,0,0,0,14,15,0,0,0,0,0,0,0,0,0,0,0,0,
I hope this is helpful.
Update: If you want the empty last field replaced as well then add an alternation to the look-ahead.
$ perl -E ' $str = q{1,2,3,,5,6,,,9,10,,,,14,15,,,,,,,,,,,,,}; $str =~ s{(?<=,)(?=,|\z)}{0}g; say $str;' 1,2,3,0,5,6,0,0,9,10,0,0,0,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0
Cheers,
JohnGG
|
|---|