tags:

views:

191

answers:

1

^\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 && 
       (condition6) 
     ) ) ) #does not match here

How can I match all the parentheses that are single?

+5  A: 

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

Noufal Ibrahim
Also, normal parser are needed, not just bracket counter.
stroncium
Not *really* if you don't care about quoted/escaped brackets. You just need a simple stack and counter.
Noufal Ibrahim