Using some extensions provided by .net, one can find groups of parenthesis by using something like this:
^(\w+)\(((?>[^()]+|\((?<D>)|\)(?<-D>))*(?(D)(?!)))\)(.*)$
This will match the following:
Func(innerfunction(arg)).DoSomething()
With the following groups:
- Group 1: Func
- Group 2: innerfunction(arg)
- Group 3: .DoSomething()
My question is, how do I match commas, taking into account if they are or not inside a parenthesis group? For example, a regex to evaluate:
Func(innerFunction(arg1, arg2), arg3).DoSomething()
Should yield:
- Group 1: Func
- Group 2: innerFunction(arg1, arg2)
- Group 3: arg3
- Group 4: .DoSomething()
Thanks.
I think I found it. Does anyone have a counter example:
This will match this expression:
as:
This solution only looks for one comma, but it deals with an arbitrary depth of parenthesis.
UPDATE: Gumbo has provided a counter-example:
Get’s split up into:
HOWEVER: By turning the first ‘any match’ into non-greedy, one can solve it:
Any other counter-example?