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

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

    Just use Text::CSV. As you can see from the source, getting CSV parsing right is quite complicated:

    sub _make_regexp_split_column {
        my ($esc, $quot, $sep) = @_;
    
        if ( $quot eq '' ) {
            return qr/([^\Q$sep\E]*)\Q$sep\E/s;
        }
    
       qr/(
            \Q$quot\E
                [^\Q$quot$esc\E]*(?:\Q$esc\E[\Q$quot$esc\E0][^\Q$quot$esc\E]*)*
            \Q$quot\E
            | # or
            [^\Q$sep\E]*
           )
           \Q$sep\E
        /xs;
    }
    
    0 讨论(0)
提交回复
热议问题