I have this string:
my $string = "2, 16, \"d4,d6\", \"d20,d22\", [0]";
and I use split like that:
my @arglist = split(/,/, $string);
The problem is that originally I never had these commas embedded in quoted string and now I have and I need to update that split regex to handle that case.
Please, absolutely don’t mention anything other than what I ask. I’ve searched enough and more than half answers are noise about how complex CSV parsing is etc etc. I didn’t write that script that I need fix and all I want is to modify that single line that does the split, so it should be simple regex that does that. There will not be any updates and the text that it splits will be this only:
CHROMA_MC_X \width, \align, "d4,d5,d6,d7", "d20,d21,d22,d23"
CHROMA_MC_X \width, \align, "d4,d6", "d20,d22", [0]
CHROMA_MC_X \width, \align, "d4,d6", "d20,d22", [0]
EDIT
The answer by Birei was enough to start from. I ended up cooking this regex that perfectly handles my case:
my @arglist = $3 =~ m/(?:(?<=")[^"]*(?=(?:\s*"\s*,|\s*"\s*$)))|(?<=,)(?:[^",]*(?=(?:\s*,|\s*$)))|(?<=^)(?:[^",]+(?=(?:\s*,|\s*$)))|(?<=^)(?:[^",]*(?=(?:\s*,)))/g;
It looks messy, but it does exactly what I need. It matches quoted lists with comas and returns them without the quote marks, there were some issues with empty args that could be present and this regex is messy because it just handles these cases as well and avoids that error with variable length lookbehind that isn’t implemented in perl regex.
What I don’t get: What’s the reason for all these downvotes, am I under attack of some perl gurus who think I don’t know what I need and what I ask for?! I have tool that does some asm preprocessing and all I needed is to handle a few cases. THAT’S IT. Thanks for help.
One way:
Content of
script.pl:Run it like:
With following result: