I'm trying to capture everything inside curly bracers, but in some cases there may be multiple bracers and I want the external ones.
For example: I want to capture
You seem to have used a character class at the end instead of a non matching group. Try:
This is a very small modification to your final attempt, that just uses correct syntax. In your final attempt you have
[$|^}]. The issue with this is that you can't have an or
| inside a character class
. Most special characters are escaped inside a character class, with a couple exceptions, one of which is
^ if it is the first character. So
[$|^}] means any of the four literal characters
}. What I did is change the syntax to what you intended by using a non-matching group
(?:stuff) this group does not save its contents and is purely for grouping. As such
(?:$|[^}]) means an end-of-line or a non-
}, as you wanted.
Note that this makes no effort to balance the curly braces (match the number of braces at the beginning and end).