To check a complete python expression use: def check_open_close(expr): try: eval(expr) except SyntaxError: return False else: return True
This also ignores brackets in quotes, and checks <= & >= operators are syntatically correct etc... But is may have side effects... ;-) eg. check_open_close('__import__("os").system("echo rm -rf /") # OK') c.f http://en.wikipedia.org/wiki/Scope_creep N -- http://mail.python.org/mailman/listinfo/python-list