如何使用RegEx获取具有特定函数名称和嵌套括号()的文本? [英] How to get text with particular function name and with nested parentheses ( ) using RegEx?
本文介绍了如何使用RegEx获取具有特定函数名称和嵌套括号()的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Reg表达式获取文本与特定的函数名和嵌套的括号,我试过,但我没有得到RegEx。对于此示例
Reg Expression for Getting Text with particular function name and with nested parentheses, I had tried but i am not getting the RegEx. For this example
如果单引号或双引号之间打开或关闭括号,请忽略。
eg: strcat("thanks for", param(add(a,b)), ' and ( valuable time ).')
Regex.Matches(script, @"(?x) \( ( (?: [^()]+ | (?<open>\() |
(?<-open>\)) )* (?(open)(?!)) ) \)");
示例: -
字符串和函数名为 param ,
if(a>b, param(add(c,d)), param(2));
Output =>
1) param(add(c,d))
2) param(2)
推荐答案
尝试此正则表达式:
\b(?!if)\w+\s*\(.+?\)(?!\)\s*,)
说明
Description
您应该使用接受括号的其他保留关键字(例如切换
)自定义此正则表达式。
You should customize this regex with other reserved keywords that accept parenthesis (switch
for instance).
\b(?!if|switch)\w+\s*\(.+?\)(?!\)\s*,)
但是,在您的情况下, C# parser 更合适。
However, in your case, a C# parser is more appropriate.
这篇关于如何使用RegEx获取具有特定函数名称和嵌套括号()的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文