our @matches; our $code_re; $code_re = qr{ ( \( (?: (?>[^()]+) | (??{$code_re}) )* \) ) (?{ push @matches, $1 }) }x; while () { chomp; @matches = (); print /^(?:(?>[^()]+)|$code_re)*$/ ? 'PASS' : (@matches = (), ' '); print " $_\n"; print " $_\n" foreach @matches; }