且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于检查字符串是否具有不匹配括号的正则表达式?

更新时间:2023-02-26 11:12:16

正则表达式不是适合该工作的工具.手动扫描字符串.

Regex is not the right tool for the job. Scan a string manually.

伪代码:

depth = 0
for character in some_string:
    depth += character == '('
    depth -= character == ')'
    if depth < 0:
       break

if depth != 0:
   print "unmatched parentheses"