I have a database with a number of fields containing comma separated values. I need to split these fields in Perl, which is straightforward enough except that some of the va
The solution you have chosen is superior, but to those who would say otherwise, regular expressions have a recursion element which will match nested parentheses. The following works fine
use strict;
use warnings;
my $s = q{recycling, environmental science, interdisciplinary (e.g., consumerism, waste management, chemistry, toxicology, government policy, and ethics), consumer education};
my @parts;
push @parts, $1 while $s =~ /
((?:
[^(),]+ |
( \(
(?: [^()]+ | (?2) )*
\) )
)*)
(?: ,\s* | $)
/xg;
print "$_\n" for @parts;
even if the parentheses are nested further. No it's not pretty but it does work!