Why does using [^{]
not work?
By using [^{]
, your regex becomes:
[^{][A-Z]+[^}]
In words, this translates to:
- character that's not a
{
- a bunch of letters
- character that's not a
}
Note how nothing in your regex enforces the idea that the "a bunch of letters" part has to be between {}
s. It just says that it has to be after a character that is not {
, and before a character that is not }
. By this logic, even something like ABC
would match because A
is not {
, B
is the bunch of letters, and C
is not }
.
How to match a word between {}
?
You can use this regex:
{([A-Z]+)}
And get group 1.
I don't think that you should combine this with the regex that matches a word after define
. You should use 2 separate regexes because these are two completely different things.
So split it into two regexes:
(?<=define |define: )\w+
and
{([A-Z]+)}