How do I split a string into an array by comma but ignore commas inside double quotes?

前端 未结 7 482
执笔经年
执笔经年 2020-12-22 07:50

I have a line:

$string = \'Paul,12,\"soccer,baseball,hockey\",white\';

I am try to split this into @array that has 4 values so

<         


        
7条回答
  •  生来不讨喜
    2020-12-22 08:08

    $string = "Paul,12,\"soccer,baseball,hockey\",white";

    1 while($string =~ s#"(.?),(.?)"#\"$1aaa$2\"#g);

    @array = map {$_ =~ s/aaa/ /g; $_ =~ s/\"//g; $_} split(/,/, $string);

    $" = "\n";

    print "$array[2]";

提交回复
热议问题