Skip to content
Advertisement

Unmatch escaped braces

Which regular expression can I use to find all }, all while excluding escaped }? I.e. how do I get as only match:

{Hello } world}
               ^

Advertisement

Answer

The following pattern should achieve your goal:

(?<!\)}

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:

{Hello } world}
               ^ with negative lookbehind
{Hello } world}
              ^^ with Amirhossein Kiani's suggestion

Note: When working with regular expressions in Python, please study the documentation. Many simple questions like this could be avoided.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement