$re = qr/(?:(?>[^()]*)|\((??{$re})\))+/; print 'I (pity (the (fool) who) dont) know (regexes)' =~ /^$re$/;