0

I'm building a parser for formulas, I'm having troubles with nested brackets, is there a simple regex to solve this kind of expressions considering an indefinite amount of nesting?

This is how the expression are composed:

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

Example of expressions:

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 }

Example of return:

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;}

With match arrays like this:

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

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

My RegEx knowledge isn't strong but I know this is achievable in some way, I hope my post is easy to understand.

I need to isolate the actions between the deeper brackets and replace them with ordered numbers.

  • 1
    "A simple regex" does not go hand in hand with "an indefinite amount of nesting" requirement. No, your question is not clear since what you want to return is different from your input. Please provide a real-life scenario. However, parsing formulas with a regex does not sound like a good idea. – Wiktor Stribiżew May 02 '17 at 10:02
  • @PetervanderHeijden: That post is not a relevant duplicate reason because .NET regex supports balanced constructs. Right now, the question is just unclear. – Wiktor Stribiżew May 02 '17 at 10:04
  • @WiktorStribiżew I tried editing to make the question more clear. Is there any doubt in particular so i can reply directly? Thanks – zombiechainsaw May 02 '17 at 10:23
  • I am afraid without your own attempt at solving this, with an explanation where it went wrong, I can't get it why you expect `return 1` to appear in the expected output if it is not present in the input. – Wiktor Stribiżew May 02 '17 at 10:28
  • @zombiechainsaw Why do you want to use Regex for this? Sounds like a job for a proper parser/grammar – Milney May 02 '17 at 10:30
  • Or if that is too much effort - you may want to look into something like this: https://msdn.microsoft.com/en-us/library/ee943825.aspx – Milney May 02 '17 at 10:31

1 Answers1

1

I solved by building a simple parser. The RegEx route is not worth it.