正则表达式搜索嵌套括号[重复]

时间:2022-09-13 07:47:39

Possible Duplicate:
Can regular expressions be used to match nested patterns?

可能重复:可以使用正则表达式来匹配嵌套模式吗?

How do I use regular expressions to search for a pair of opening and closing brackets with arbitrary number of nested brackets inside it e.g.

如何使用正则表达式搜索其中包含任意数量的嵌套括号的一对开始和结束括号,例如

(...(...(...(...) ...) ...) ...)

Each opening bracket must be matched by a closing bracket. And the number of nested opening-closing pair is arbitrary. Other alphanumeric characters may appear inside the brackets.

每个开口支架必须与一个闭合支架相匹配。并且嵌套的开 - 关对的数量是任意的。括号内可能出现其他字母数字字符。

1 个解决方案

#1


1  

If you really want to use RegEx to do it(i'm not sure it's the good way), you can loop until this regex \([^\(\)]*\) returns nothing, and for each times you get something you have to remove the result in the initial and loop again...

如果你真的想用RegEx来做它(我不确定这是好方法),你可以循环直到这个正则表达式\([^ \(\)] * \)什么都不返回,并且你每次都得到一些东西你必须删除初始中的结果并再次循环...

#1


1  

If you really want to use RegEx to do it(i'm not sure it's the good way), you can loop until this regex \([^\(\)]*\) returns nothing, and for each times you get something you have to remove the result in the initial and loop again...

如果你真的想用RegEx来做它(我不确定这是好方法),你可以循环直到这个正则表达式\([^ \(\)] * \)什么都不返回,并且你每次都得到一些东西你必须删除初始中的结果并再次循环...