2010-01-25 79 views
0

^\s*[)]*\s*$^\s*[(]*\s*$与粗体的圆括号()相符。也就是说,我是什么想的忽略是单身,而不是(条件1)括号内括号:如何匹配Perl中的某些嵌套圆括号?

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

) #matches here 

但如果我有这样的不匹配:

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

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

如何匹配所有单个括号?

+0

请给我们一些数据! – 2010-01-25 11:41:57

+0

所以基本上你想匹配所有的括号,除了最内层的那个,我说得对吗? – Amarghosh 2010-01-25 11:43:00

+3

你应该真的使用解析器。正则表达式不能用于表达非常规语言,如你的。 – Gumbo 2010-01-25 11:44:13

回答

5

我个人建议您使用一个简单的栈来计算开放和闭括号,而不是在正则表达式之间跳过。

+1

另外,需要普通的解析器,而不仅仅是支架计数器。 – stroncium 2010-01-25 11:14:44

+1

不*真的*如果你不关心引用/转义括号。你只需要一个简单的堆栈和计数器。 – 2010-01-25 11:37:19