开发者

regex embedded {{ matching

开发者 https://www.devze.com 2023-03-05 17:10 出处:网络
I need to match the entire following statement: {{CalendarCustom|year={{{year|{{#time:Y}}}}}|month=0开发者_JS百科8|float=right}}

I need to match the entire following statement:

{{CalendarCustom|year={{{year|{{#time:Y}}}}}|month=0开发者_JS百科8|float=right}}

Basically whenever there is a { there needs to be a corresponding } with however many embedded { } are inside the original tag. So for example {{match}} or {{ma{{tch}}}} or {{m{{a{{t}}c}}h}}.

I have this right now:

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

This does not quite work.


The .NET regex engine 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;


I suggest writing a simple parser/tokenizer for this.

Basically, you loop over all the characters and start counting instances of { and } - incrementing for { and decrementing for }. Record the index of each first { and the index of each last } and you will have the indexes for your embedded expressions.

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

See this question and answers for why RegEx is not suitable.

0

精彩评论

暂无评论...
验证码 换一张
取 消