Regex embedded {{matches

I need to match the entire following statement:

{{CalendarCustom|year={{{year|{{#time:Y}}}}}|month=08|float=right}} 

In principle, when there is { , there must be a corresponding } , but many built-in { } are inside the original tag. For example, {{match}} or {{ma{{tch}}}} or {{m{{a{{t}}c}}h}} .

I have it right now:

 (\{\{.+?(:?\}\}[^\{]+?\}\})) 

This does not work.

+7
source share
2 answers

The regex.NET mechanism allows recursive matching:

 result = Regex.Match(subject, @"\{ # opening { (?> # now match... [^{}]+ # any characters except braces | # or \{ (?<DEPTH>) # a {, increasing the depth counter | # or \} (?<-DEPTH>) # a }, decreasing the depth counter )* # any number of times (?(DEPTH)(?!)) # until the depth counter is zero again \} # then match the closing }", RegexOptions.IgnorePatternWhitespace).Value; 
+15
source

I suggest writing a simple parser / tokenizer for this.

Basically, you iterate over all characters and start counting instances of { and } - increase for { and decrease for } . Write down the index of each first { and the index of each last } , and you will have indexes for the built-in expressions.

At this point, you can use substring to get them and remove / replace from the original string.

See this question and answers why RegEx is not suitable.

+4
source

All Articles