I'm looking for a Regex which can do this : My text :
"Blablabla {{ blabla1 }} blablablabla {{ blablabla2 {{ blabla3 }} }} blablabla"
What I want to extract :
"blabla1" and "blablabla2 {{ blabla3 }}"
Does anyone has an idea ?
I tried with : "{{(.)*}}"
but it returns "blabla1"
and "blabla3"
You can use balancing groups for counting and matching nested constructs like these. For example:
(?x) {{ ( (?: [^{}]+ | (?<open>{{) | (?<-open>}}) )* (?(open)(?!)) ) }}
This has nesting, so it’s not a regular grammar. Some regex engines have extensions to handle brace matching, but in general the best way to do this is by simply scanning the string and accumulating output in a List<string>
while keeping track of the nesting depth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With