Regex matches everything between two {}

I looked at different answers here, but, unfortunately, none of them were good for my business. Therefore, I hope you are not against it.

Therefore, I need to match everything between two curly braces {}, except when the match begins with @ and without these curly braces, for example:

  • "This is super text { match_this }"
  • "{ match_this }"
  • "This is another example @ {deal_with_it}"

Here are my test lines, 1,2,3 are valid, and the latter should not be:

  1   {eww}
  2   r23r23{fetwe}
  3   #{d2dded}
  4   @{d2dded}

I have tried:

(?<=[^@]\{)[^\}]*(?=\})

Then only the 2nd and 3rd options were coincidences (without the first) https://regex101.com/r/qRInUf/2/

Then I tried:

\{(.*?)\} or [^@]\{(.*?)\}

1,2,3 https://regex101.com/r/uYRtLD/1

.

EDIT: java.

+4
2

. regex

(?<=(?<!@)\{)[^}]*(?=})
  • (?<=(?<!@)\{) lookbehind, , ,
    • (?<!@) lookbehind, , , @
    • \{ { .
  • [^}]* }
  • (?=}) , , }

:

{eww}           # Matches eww
r23r23{fetwe}   # Matches fetwe
#{d2dded}       # Matches d2dded
@{d2dded}       # Does not match this because @ precedes {
+6

:

(?<!@\{)(?<=\{).*?(?=\})

, @{, lookbehind, { , }.

.

+3

Source: https://habr.com/ru/post/1695250/


All Articles