正则表达式{{匹配

thi*_*y93 7 c# regex

我需要匹配以下整个语句:

{{CalendarCustom|year={{{year|{{#time:Y}}}}}|month=08|float=right}}
Run Code Online (Sandbox Code Playgroud)

基本上每当有原始标签内部{需要有相应数量的}嵌入时{ }.所以例如{{match}}或者{{ma{{tch}}}}{{m{{a{{t}}c}}h}}.

我现在有这个:

(\{\{.+?(:?\}\}[^\{]+?\}\}))
Run Code Online (Sandbox Code Playgroud)

这不太奏效.

Tim*_*ker 15

.NET正则表达式引擎允许递归匹配:

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;
Run Code Online (Sandbox Code Playgroud)