首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Regex原子开闭括号

C# Regex原子开闭括号
EN

Stack Overflow用户
提问于 2019-11-24 04:34:50
回答 1查看 56关注 0票数 1

我有以下占位符,我试图使用原子组成对捕获双开括号和尾括号:

{{(?>{{((?)\{}}(<-纵深>)\{}^}*}]((深度)(?)) 我是{名字},我有{{刻度}:红色{{metallic_scales}:闪闪发光}}

可以肯定地说,我并没有取得多大的成功。

所以我想要这样的东西:

  1. {名称}
  2. {鳞片}:红色{metallic_scales}:闪闪发光}鳞片}}
  3. {{metallic_scales}:闪闪发光的}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-24 12:09:06

你可以用

代码语言:javascript
复制
(?=                    # Start of a positive lookahead to enable overlapping matches
  (?<results>          # The group storing the results
    {{                 # {{ (left-hand delimiter)
      (?>              # Start of an atomic group
        (?!{{|}}).|    # Any char not starting {{ or }} char sequence, or
        {{(?<DEPTH>)|  # {{ and a value pushed on to DEPTH group stack, or
        }}(?<-DEPTH>)  # }} and a value popped from the DEPTH group stack
      )*               # Zero or more repetitions of the atomic group patterns
    }}                 # }} substring
    (?(DEPTH)(?!))     # Conditional construct failing the match if DEPTH stack is not empty
  )                    # End of the results group
)                      # End of the lookahead

regex演示

C#声明:

代码语言:javascript
复制
Regex reg = new Regex(@"
    (?=                  # Start of a positive lookahead to enable overlapping matches
      (?<results>          # The group storing the results
        {{                 # {{ (left-hand delimiter)
          (?>              # Start of an atomic group
            (?!{{|}}).|    # Any char not starting {{ or }} char sequence, or
            {{(?<DEPTH>)|  # {{ and a value pushed on to DEPTH group stack, or
            }}(?<-DEPTH>)  # }} and a value popped from the DEPTH group stack
          )*               # Zero or more repetitions of the atomic group patterns
        }}                 # }} substring
        (?(DEPTH)(?!))     # Conditional construct failing the match if DEPTH stack is not empty
      )                    # End of the results group
    )                      # End of the lookahead", 
    RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
var results = reg.Matches(text).Cast<Match>().Select(x => x.Groups["results"].Value);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59014693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档