简体   繁体   中英

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

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

Here, all brackets would match but the last 2.

Edited based on comments:

Here you go:

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

Demo

Explanation:

  • Match { if what follows is not {0} or {1} .
  • Match } if what precedes is not {0 or {1 .

The matches in the following sample are bolded:

{ 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

You can use negative lookhead based regex :

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

Online regex Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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