Matching with Opening/Closing Pair Characters in Regular Expressions

前端 未结 1 1077
余生分开走
余生分开走 2021-01-07 07:10

First, a little background. I have strings that resemble the following:

((Foo.Bar.StartsWith(\"A\")) && (Foo.Bar.EndsWith(\"B\")))

相关标签:
1条回答
  • 2021-01-07 07:33

    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.

    0 讨论(0)
提交回复
热议问题