.NET Regex balancing groups expression - matching when not balanced

后端 未结 2 1110
灰色年华
灰色年华 2021-01-26 23:55

.NET balanced group regexes make my head explode. I\'ve got this string i\'m trying to match:

other stuff blah blah....
                    {
                           


        
相关标签:
2条回答
  • 2021-01-27 00:39

    Have you checked what it is matching in the second case? Since you don't have any anchors, I think the engine starts the match just after the first '{'. From there, until the end, the string matches.

    Try surrounding the pattern with \A and \z.

    0 讨论(0)
  • 2021-01-27 00:39

    Not to be a kill joy but what you're attempting to do with a regular expression is boggling your mind because it's simply not possible. Regular expressions are a class of finite automata and do not posses enough state in which to peform recursive/nested matching. You'll need a context free grammar of sorts in order to get this working.

    There are some regular expression engines which do support a notion of recursion. These are not strictly speaking regular expressions though. Can you tell us what engine you are using because it's possible it has a recursion function which will help out this scenario.

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