-1

I have a mathematical expression (((a+b)(c+d))+p(q*2+r))/2 I am trying to write a C# code to match the groups as below

  1. (((a+b)(c+d))+p(q*2+r))
  2. ((a+b)(c+d))
  3. (q*2+r))
  4. (a+b)
  5. (c+d))

Unfortunately I am not able to find one. Can you please help me from this situation

I have an expression "((.*?))" but this can match as below and it is not matching with my need

  1. (((a+b)
  2. (c+d)
  3. (q*2+r)

NB: Expression can be changed. Looking for a generic regex pattern to parse any expression with BODMAS rule

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Hari
  • 9
  • 1
  • 3
    This should be done as a parser, rather than regex. – VLAZ Nov 22 '20 at 14:29
  • Use the [Balancing Group definitions](https://docs.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#balancing_group_definition). – liori Nov 22 '20 at 14:38

1 Answers1

-2

this will do the job :

(\((\((\(.*?\))(\(.*?\))\)).*(\(.*\))\))

this regex will split it into groups then you can get each group from it.

this is a demo

aziz k'h
  • 743
  • 2
  • 10