my $NestedGuts = qr{ (?{ local $OpenParens = 0 }) # Counts the number of nested opens waiting to close. (?> # atomic-grouping for efficiency (?: # Stuff not parenthesis [^()]+ # An opening parenthesis | \( (?{ $OpenParens++ }) # Allow a closing parenthesis, if we're expecting any | \) (?(?{ $OpenParens != 0 }) (?{ $OpenParens-- }) | (?!) ) )* ) (?(?{ $OpenParens != 0 })(?!)) # If there are any open parens left, don't finish }x; ----------------------------------------------------------------------------- Copyright 1997-2024 Jeffrey Friedl