简体   繁体   English

正则表达式:匹配表达式{0}或{1}中没有的所有括号{}

[英]Regex: Match all brackets {} that are not in expressions {0} or {1}

The title says all, I am trying to match all { and } but not those included in {0} or {1} . 标题说明了所有问题,我正在尝试匹配所有{}但不匹配{0}{1}

Ex: {asd } {2} asds}w2{1} 例如: {asd } {2} asds}w2{1}

Here, all brackets would match but the last 2. 在这里,所有括号都将匹配,但最后一个是2。

Edited based on comments: 根据评论进行编辑:

Here you go: 干得好:

(?!\{[01]\})\{|(?<!\{[01])\}

Demo 演示版

Explanation: 说明:

  • Match { if what follows is not {0} or {1} . 如果后面不是{0}{1}则匹配{
  • Match } if what precedes is not {0 or {1 . 如果前面不是{0{1则匹配}

The matches in the following sample are bolded: 以下示例中的匹配项以粗体显示:

{ asd } { 2 } asds } w2{1} { foo } {0} { bar } } { { } { asd } { 2 } asds } w2 {1} { foo } {0} { bar } } { { }

You could try this regex, 您可以尝试此正则表达式,

(?:\{0\}|\{1\})(*SKIP)(*F)|(?:{|})

DEMO 演示

OR 要么

(?<!0|1)}|{(?!1|0)

DEMO 1 , DEMO 2 演示1演示2

You can use negative lookhead based regex : 您可以使用基于负号的正则表达式

\{(?![12]\})[^}]*\}

Online regex Demo 在线正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM