use strict; use warnings; use 5.010; my $str = '((mouse,rat),(human,chimp))'; my $str2 = '(mouse,(human,chimp))'; my @buffers; for ($str, $str2) { @buffers = ([]); m[ ^ ( (?: (\w+) # This serves only to get out fast if we parse # something bad (e.g. where closing parens are # missing) (*PRUNE) (?{ push @{$buffers[-1]}, $2; }) | \( (?{ push @{$buffers[-1]}, my $new = []; push @buffers, $new; }) (?1) (?{ pop @buffers; }) \) ) (?: , (?1) )? ) \z ]x ? do { use Data::Dumper; print Dumper $buffers[0]; } : do { warn "no match: $_\n"; }; }