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

前端 未结 7 519
执笔经年
执笔经年 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:22

    use strict;
    use warning;
    #use Data::Dumper;
    
    my $string = qq/Paul,12,"soccer,baseball,hockey",white/;
    
    #split string into three parts
    my ($st1, $st2, $st3) = split(/,"|",/, $string);
    #output: st1:Paul,12 st2:soccer,baseball,hockey  st3:white  
    
    #split $st1 into two parts
    my ($st4, $st5) = split(/,/,$st1);
    
    #push records into array
    push (my @test,$st4, $st5,$st2, $st3 ) ;
    
    #print Dumper \@test;
    print "$test[2]\n";
    

    output:

    soccer,baseball,hockey 
    
    #$VAR1 = [
    #          'Paul',
    #         '12',
    #          'soccer,baseball,hockey',
    #          'white'
    #        ];
    

提交回复
热议问题