Regex to capture {}

Hi, I need to create a regex that matches the following pattern:

{any characters} per line. Possible string examples:

Example 1:

{123} abc {456} def {789} hg must return 3 matches:

{123}, {456} and {789}

Example 2:

abcd {{{r} 345} {mmm}

should return 2 mathces {r} and {mmm}

So basically the expression should match {any chacraters any number of times that is not {}, and then close}

So far I have come up with this, but it does not work:

{* [^ {]}

Thank you very much in advance

+4
source share
2 answers

Here you need to:

{[^{}]*} 
+3
source

Like what ILovePaperTowels said, you need to escape curly braces if they are treated as special using the specific regular expression syntax that you use.

The exact answer on how to avoid curly braces, as well as how to correctly count the enclosed brackets (no matter how you take them into account), depends on the aroma of the regular expression.

+2
source

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


All Articles