如何使用正则表达式匹配嵌套函数调用(括号对)(递归?) [英] How to match nested function invocations (bracket pairs) using a regular expression (recursive?)

查看:825
本文介绍了如何使用正则表达式匹配嵌套函数调用(括号对)(递归?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个与未知数量的嵌套函数匹配的正则表达式(**)。
所以

I'm looking for a regular expression (**) that will match an unknown number of nested functions. So

expression
function(expression)
function(function(expression))
function(function(function(expression)))
etc.

将全部匹配成功。但是,例如,如果我在末尾添加一个额外的结束括号,它将不包括在比赛中。

will all match successfully. But for instance if I add an extra closing bracket at the end it wouldn't be included in the match.

(**)请不要回答这个问题。通过解析(和计数括号)而不是使用正则表达式更容易做到 - 在我抓了一会儿之后我已经知道了!

(**) Please don't answer that this would be easier to do by parsing (and counting brackets) rather than using a regular expression - after scratching my head for a while I know that already!

推荐答案


我正在寻找一个与未知数量的嵌套函数匹配的正则表达式(**)。

I'm looking for a regular expression (**) that will match an unknown number of nested functions.

一些正则表达式实现支持递归匹配(Perl,PHP,.NET),但JavaScript不支持。所以,你的问题的答案是:不,这是不可能的。

Some regex implementations support recursive matching (Perl, PHP, .NET), but JavaScript does not. So, the answer to your question is: no, that is not possible.

这篇关于如何使用正则表达式匹配嵌套函数调用(括号对)(递归?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆