First, a little background. I have strings that resemble the following:
((Foo.Bar.StartsWith(\"A\")) && (Foo.Bar.EndsWith(\"B\")))
Is there a reason you have to do this with regular expressions? Trying to find "matching" pairs is one of the things that regex does not do well.
I would just do it through normal code. Do a search to find "(Foo."
, then make a loop that iterates over the subsequent characters, keeping a counter for how many open/close parentheses you have passed. Increment it when you hit a (
and decrement when you hit a )
. If you hit a closing parenthesis and the counter is at 0, that's the end of the string you need to capture. Stop capturing at that point, take that string and put it inside your Any()
that you're adding. Then find the next "(Foo."
.
Does that make sense? I could probably pseudocode it if you're not really following what I mean. Of course, this is only a valid option if you're able to abandon regular expressions anyway.