my $str = "((A,B,(C,D,(E,F),G),H),(I,J,K))"; our @nodes; our $re; $re = qr{ \( (?: (?: ((?> [^()]+ )) # Non-parens without backtracking (?{ /./ }) # <--- any regex here segfaults )+ | (??{ $re }) # Group with matching parens )* \) }x;