Which regular expression can I use to find all }
, all while excluding escaped }
?
I.e. how do I get as only match:
JavaScript
x
3
1
{Hello } world}
2
^
3
Advertisement
Answer
The following pattern should achieve your goal:
JavaScript
1
2
1
(?<!\)}
2
The pattern you’re looking for is (?<!...)
:
Matches if the current position in the string is not preceded by a match for …. This is called a negative lookbehind assertion.
In contrast to Amirhossein Kiani’s suggestion, the match does not include the preceding character, but only the closing bracket. To illustrate:
JavaScript
1
5
1
{Hello } world}
2
^ with negative lookbehind
3
{Hello } world}
4
^^ with Amirhossein Kiani's suggestion
5
Note: When working with regular expressions in Python, please study the documentation. Many simple questions like this could be avoided.