.NET balanced group regexes make my head explode. I’ve got this string i’m trying to match:
other stuff blah blah.... { stuff stuff {key: stuff stuff } } more stuff.....
Here’s my regex:
[^{}]* # anything that isn't { } \{ # starting with { (?> # atomic group: throw away backtracks on exit [^{}]+ | \{(?:\w+:)?(?<DEPTH>) # on matching { plus optional 'word:' push stack | \}(?<-DEPTH>) # on matching } pop stack )* (?(DEPTH)(?!)) # if depth unbalanced, fail match \} # ending with } [^{}]* # anything that isn't { }
So, I’m trying to match balancing curly braces, where some of the opening curly braces have an optional word followed by a colon. The above regex matches my example string, but if i remove a curly brace, (i.e. ‘unbalancing it):
other stuff blah blah.... { stuff stuff {key: stuff stuff } more stuff.....
…it still matches!
Can anyone tell me how to fix my regex?
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.