{}think has asked for the wisdom of the Perl Monks concerning the following question:
Monks,
I am trying to edit a CSV string, where my goal is to insert a character (a zero) between any occurrence of two consecutive commas. In other words, I'm replacing any missing values with a zero, e.g.,
1) What regex would accomplish this, and
2) What is wrong with my approach. There's some subtlety of regexs that I need to familiarize myself with!
I am trying to edit a CSV string, where my goal is to insert a character (a zero) between any occurrence of two consecutive commas. In other words, I'm replacing any missing values with a zero, e.g.,
1,2,,4 -> 1,2,0,4The regex I have written does not work as I expected, so I have two questions:
1) What regex would accomplish this, and
2) What is wrong with my approach. There's some subtlety of regexs that I need to familiarize myself with!
This code:
$s="1,2,3,,5,6,,,9,10,,,,14,15,,,,,,,,,,,,,"; $s=~s/,,/,0,/g; print "$s\n"; '...results in :
1,2,3,0,5,6,0,,9,10,0,,0,14,15,0,,0,,0,,0,,0,,0,,Thanks for considering this issue!
{}think; #Think outside of the brackets
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regex to replace consecutive tokens
by johngg (Canon) on Oct 18, 2013 at 12:30 UTC | |
Re: Regex to replace consecutive tokens
by Corion (Patriarch) on Oct 18, 2013 at 12:44 UTC | |
by choroba (Cardinal) on Oct 18, 2013 at 12:47 UTC | |
by wjw (Priest) on Oct 18, 2013 at 14:44 UTC | |
by BrowserUk (Patriarch) on Oct 18, 2013 at 14:53 UTC | |
Re: Regex to replace consecutive tokens
by hdb (Monsignor) on Oct 18, 2013 at 12:32 UTC | |
Re: Regex to replace consecutive tokens
by Bloodnok (Vicar) on Oct 18, 2013 at 13:00 UTC | |
Re: Regex to replace consecutive tokens
by roboticus (Chancellor) on Oct 18, 2013 at 13:19 UTC | |
by hdb (Monsignor) on Oct 18, 2013 at 13:24 UTC | |
by choroba (Cardinal) on Oct 18, 2013 at 13:44 UTC | |
Re: Regex to replace consecutive tokens
by oiskuu (Hermit) on Oct 18, 2013 at 22:54 UTC |
Back to
Seekers of Perl Wisdom