in reply to split on the pattern of '),('

Assuming nesting (e.g. (3,(4,5),6)) can't happen,

my @recs = $recs =~ / \G \( ( (?: [^()"] | "[^"]*" )* ) \) (?: , | \Z ) /xg;

Replies are listed 'Best First'.
Re^2: split on the pattern of '),('
by tobyink (Canon) on Jun 08, 2012 at 07:17 UTC
    my $string = "(foo),(bar),(baz)"; my @parts = split m{ [)] [,] [(] }x, $string; print "GOT: $_\n" for @parts;

    Instead of m{ [)] [,] [(] }x you could use m{ \) , \( }x ... whichever you think is most readable. Either way, try to avoid the appearance of a million toothpicks standing on end.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      That fails for
      $str = '(1,"(text),(text)", 123),(2,"(string)", 234),(...)';

        No it doesn't. The string you give contains "),(" three times, and my regex splits it at all three.

        use Test::More tests => 5; my $string = '(1,"(text),(text)", 123),(2,"(string)", 234),(...)'; my @parts = split m{ [)] [,] [(] }x, $string; # Prove that the split worked properly... # Firstly, joining the parts back with '),(' recreates the original st +ring. my $joined = join '),(', @parts; is($joined, $string); # Secondly, none of the individual parts contain '),(' unlike($_, qr{\Q),(\E}) for @parts; done_testing();
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'