正则表达式嵌入式{{匹配 [英] regex embedded {{ matching

查看:145
本文介绍了正则表达式嵌入式{{匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要匹配整个下面的语句:

I need to match the entire following statement:

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

基本上只要有是一个 {需要有一个相应的} 与然而,许多嵌入式 {} 是原始标签内。因此,例如 {{匹配}} {{MA {{TCH}}}} {{M-{{一{【T】}℃}} h}可以}

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}}.

我现在有这个权利。

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

这不是做得比较工作。

推荐答案

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

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;

这篇关于正则表达式嵌入式{{匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆