Well, an old question, but I just had to struggle with it all night, and there was never an answer to the question, so if someone comes here Google, like me, that's what I finally got. This is a very short answer using only the built-in PERL regular expression functions:
my $string='ab,12,20100401,xyz(A,B)'; string =~ 's/((\((?>[^)(]*(?2)?)*\))|[^,()]*)(*SKIP)([,])/$1\n/g'; my @array=split('\n',$string);
Commas that are not included in parentheses are changed to new lines, and then arrays are divided into them. This will ignore the commas inside any level of nested parentheses if they are correctly balanced with the appropriate number of open and closed pairs.
It is assumed that in the initial value of $ string there will be no newline characters \n . If you need to either temporarily replace them with something else in front of the lookup line, then use a loop to replace after split or just select another separator to split the array.
Michael Kupietz
source share