Alternately pull SIN(X) from the expression SIN (SIN(20 + 20)) + COS (20)

The problem is that the regular must pull out the first match with a paired number of parentheses. That is, according to the logic of the program, from this expression it should draw

0:SIN(20 + 20)
1:20+20

Or another example:

SIN(20) + SIN(SIN(30) - SIN(40))

Must pull:

0:SIN(20)
1:20

And from

SIN(20 - SIN(40))

Must pull:

0:SIN(40)
1:40

C#, .NET language 4.0!

Author: angry, 2011-05-18

2 answers

Try this way:

SIN\(([^()]+)\)
 3
Author: yozh, 2011-05-18 21:24:42

Ai, such problems are not solved by regular rules, do it better on finite automata.

UPD

Yes, there is a wonderful algorithm called "recursive descent" on which you can build a parser of mathematical expressions, with variable functions and other joys, try Googling and implementing it. If necessary and I have time , I will explain this algorithm with examples

 2
Author: Alex Kapustin, 2011-05-19 13:36:34