正则表达式提取函数名称,及其参数

Uma*_*ngo 6 c# regex

我正在构建一个应用程序,用户可以在其中为某些字段指定表达式。表达式也应包含函数。我需要评估这些表达式并在报告中显示最终值。

我有一个表达式来提取函数名称及其参数。以前,函数参数是十进制值。但是现在,参数也可以是表达式。

例如,

Round( 1  * (1+  1 /100) % (2 -1), 0)

Function-name : Round
Parameter1    : 1  * (1+  1 /100) % (2 -1)
Parameter2    : 0
Run Code Online (Sandbox Code Playgroud)

上一个正则表达式:

string pattern2 = @"([a-zA-Z]{1,})[[:blank:]]{0,}\(([^\(\)]{0,})\)";
Run Code Online (Sandbox Code Playgroud)

这个正则表达式不再帮助我找到表达式参数。

有人可以帮助我使用正确的正则表达式来提取函数名称和参数吗?我实现了 Math 类支持的全部或大部分功能。该程序是用c#构建的

在此先感谢您的帮助。

Ken*_*ent 3

 "^\s*(\w+)\s*\((.*)\)"
Run Code Online (Sandbox Code Playgroud)

group(1) 是函数名称

拆分组(2),","您将获得参数列表。

更新

由于我没有Windows系统(.Net),所以我用python进行测试。嵌套函数不是问题。如果我们在表达式的开头添加“^\s*”:

import re

s="Round(floor(1300 + 0.234 - 1.765), 1)"
m=re.match("^\s*(\w+)\s*\((.*)\)",s)
m.group(1)
Output: 'Round'

m.group(2)
Output: 'floor(1300 + 0.234 - 1.765), 1'
you can split if you like:
m.group(2).split(',')[0]
Out: 'floor(1300 + 0.234 - 1.765)'

m.group(2).split(',')[1]                                                                                                        
Out: ' 1'
Run Code Online (Sandbox Code Playgroud)

好吧,如果你的函数嵌套是这样的f(a(b,c(x,y)),foo, m(j,k(n,o(i,u))) ),我的代码将无法工作。