开发者

How can I match certain nested parentheses in Perl?

开发者 https://www.devze.com 2022-12-17 06:01 出处:网络
^\\s*[)]*\\s*$and ^\\s*[(]*\\s*$ matches the parentheses ( and ) which are bold. That is, what am trying is to ignore parentheses that are single and not (condition1) parentheses:

^\s*[)]*\s*$ and ^\s*[(]*\s*$ matches the parentheses ( and ) which are bold. That is, what am trying is to ignore parentheses that are single and not (condition1) parentheses:

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

  ) #matches here

but if I have like this it does not match:

while
 (( #does not match here
      (condition1) && (condition2) && 
       condition3 
    ) || 
    (
      (condition4) || 
       condition5 && 
       (condition6) 
     ) ) #does not match here

or

while
 ((( #does not match here
      (condition1) && (condition2) && 
       condition3 
    )) || 
    ((  #does not match here
      (condition4) || 
       condition5 && 开发者_StackOverflow
       (condition6) 
     ) ) ) #does not match here

How can I match all the parentheses that are single?


I'd personally recommend that you use a simple stack to figure out open and closing brackets rather than trip over regular expressions.

0

精彩评论

暂无评论...
验证码 换一张
取 消