Complicated regular expression

I

Ian Hannah

I have an equation like follows:

choose(condition, choose(condition, x, y), z)

which can have any number of choose statements embeded. The parts x, y and z
can themselves contain commas but these will be in brackets e.g

choose(condition, choose(condition, data(x,2), data(y,2) + data(y,3)), z)

For each of the two main arguments to the choose statement I want to replace
the item with some other additional text e.g.

choose(condition, choose(condition, func(data(x,2)), func(data(y,2) +
data(y,3))), func(z))

Does anyone know how to do this with a regular expression in C#?

Thanks
Ian H

J

Jeroen Mostert

Ian said:
I have an equation like follows:

choose(condition, choose(condition, x, y), z)

which can have any number of choose statements embeded. The parts x, y and z
can themselves contain commas but these will be in brackets e.g

choose(condition, choose(condition, data(x,2), data(y,2) + data(y,3)), z)

For each of the two main arguments to the choose statement I want to replace
the item with some other additional text e.g.

choose(condition, choose(condition, func(data(x,2)), func(data(y,2) +
data(y,3))), func(z))

Does anyone know how to do this with a regular expression in C#?
"Some people, when confronted with a problem, think 'I know, I'll use
regular expressions.' Now they have two problems." --jwz

A regular expression isn't powerful enough to do this; the problem is that
arguments can have indefinite nesting. You'll need a context-free parser at
least to solve this. Those don't have to be complicated; a simple
left-recursive descent parser can do it, or you can use one of the many
frameworks available.