I have a line:
$string = \'Paul,12,\"soccer,baseball,hockey\",white\';
I am try to split this into @array that has 4 values so
<
Use this regex: m/("[^"]+"|[^,]+)(?:,\s*)?/g;
The above regular expression globally matches any word that starts with a comma or a quote and then matches the remaining word/words based on the starting character (comma or quote).
Here is a sample code and the corresponding output.
my $string = "Word1, Word2, \"Commas, inbetween\", Word3, \"Word4Quoted\", \"Again, commas, inbetween\"";
my @arglist = $string =~ m/("[^"]+"|[^,]+)(?:,\s*)?/g;
map { print $_ , "\n"} @arglist;
Here is the output:
Word1
Word2
"Commas, inbetween"
Word3
"Word4Quoted"
"Again, commas, inbetween"