$pattern = '{ # The regular expression begins here . . . ^ (?P # Everything within this set of parentheses is named "stuff." (?: [^()]++ # anything not parentheses | \( (?P>stuff) \) # an open paren, more "stuff," and finally a close paren. )* ) $ # This is the end of the regular expression. }x'; # The 'x' here is a preg pattern modifier. if (preg_match($pattern, $text)) echo "text is balanced\n"; else echo "text is unbalanced\n"; ----------------------------------------------------------------------------- Copyright 1997-2024 Jeffrey Friedl