如何匹配Perl中的某些嵌套括号?

Use*_*611 0 regex perl

^\s*[)]*\s*$^\s*[(]*\s*$匹配括号()粗体.也就是说,我想要的是忽略单个而不是(condition1)括号的括号:

while
   (    #matches here
     (  #matches here
      (condition1) && (condition2) && 
       condition3 
    ) || 
    (#matches here
      (condition4) || 
       condition5 && 
       (condition6) 
     ) #matches here

  ) #matches here
Run Code Online (Sandbox Code Playgroud)

但如果我喜欢这个,它就不匹配:

while
 (( #does not match here
      (condition1) && (condition2) && 
       condition3 
    ) || 
    (
      (condition4) || 
       condition5 && 
       (condition6) 
     ) ) #does not match here
Run Code Online (Sandbox Code Playgroud)

要么

while
 ((( #does not match here
      (condition1) && (condition2) && 
       condition3 
    )) || 
    ((  #does not match here
      (condition4) || 
       condition5 && 
       (condition6) 
     ) ) ) #does not match here
Run Code Online (Sandbox Code Playgroud)

如何匹配所有单个括号?

Nou*_*him 5

我个人建议你使用一个简单的堆栈来计算开放和结束括号而不是跳过正则表达式.