Split on comma, but only when not in parenthesis

后端 未结 6 1343
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 23:42

I am trying to do a split on a string with comma delimiter

my $string=\'ab,12,20100401,xyz(A,B)\';
my @array=split(\',\',$string);

If I do

6条回答
  •  爱一瞬间的悲伤
    2021-01-02 23:56

    Here is one way that should work.

    use Regexp::Common;
    
    my $string = 'ab,12,20100401,xyz(A,B)';
    my @array = ($string =~ /(?:$RE{balanced}{-parens=>'()'}|[^,])+/g);
    

    Regexp::Common can be installed from CPAN.

    There is a bug in this code, coming from the depths of Regexp::Common. Be warned that this will (unfortunately) fail to match the lack of space between ,,.

提交回复
热议问题