嵌套括号RegEx C#

时间:2017-05-02 09:57:28

标签: c# .net regex regex-group

我正在为公式构建一个解析器,我遇到了嵌套括号的麻烦,考虑到无限量的嵌套,是否有一个简单的正则表达式来解决这种表达式?

这是表达式的组成方式:

If(Condition) Then { Action } Else { Action2 }

表达式示例:

1: if (Category = 4) then {Validation(HB):Insert(COMNP)} else {Nothing}

2: if (Requested(INDP)) then {  if (Result(INDP) > 70)  then {   DoNothing   }  else { MakeSomething } else { MakeSomethingElse }

返回示例:

1: if (Category = 4) then {return 1;} else {return 2;}

2: if (Requested(INDP)) then {  if (Result(INDP) > 70)  then {   return 1; }  else { return 2; } else { return 3;}

使用这样的匹配数组:

[1]: Validation(HB):Insert(COMNP) 
[2]: Nothing

[1]: DoNothing 
[2]: MakeSomething 
[3]: MakeSomethingElse 

我的RegEx知识不强,但我知道这在某种程度上是可以实现的,我希望我的帖子很容易理解。

我需要隔离更深层括号之间的动作,并用有序数字替换它们。

1 个答案:

答案 0 :(得分:0)

我通过构建一个简单的解析器来解决。 RegEx路线不值得。