#!/usr/bin/perl $teststr="blah,blah(blah,blah(blah,blah(blah))),blah"; #This is three elements: # blah # blah(blah,blah(blah,blah(blah))) # blah # I don't have to worry about escaped parens, the file format forbids it. foreach (&parse_comma($teststr)){ print "$_\n"; #This just proves that it works } sub parse_comma{ my $commastr=shift; my @tags; my $count=0; my $carrystr=""; foreach (split(/,/, $commastr)){ $_=$carrystr.",".$_ if $carrystr; $count=s/\(/(/g; $count-=s/\)/)/g; if($count){ $carrystr=$_; }else{ $carrystr=""; push @tags, $_; } } return @tags; }