I would like to make a regular expression for formatting a text, in which there can't be a {
character except if it's coming with a backslash \
behind. The problem is that a backslash can escape itself, so I don't want to match \\{
for example, but I do want \\\{
. So I want only an odd number of backslashs before a {
. I can't just take it in a group and lookup the number of backslashs there are after like this:
s = r"a wei\\\{rd thing\\\\\{"
matchs = re.finditer(r"([^\{]|(\\+)\{)+", s)
for match in matchs:
if len(match.group(2)) / 2 == len(match.group(2)) // 2: # check if it's even
continue
do_some_things()
Because the group 2 can be used more than one time, so I can access only to the last one (in this case, \\\\\
)
It would be really nice if we could just do something like "([^\{]|(\\+)(?if len(\2) / 2 == len(\2) // 2)\{)+"
as regular expression, but, as far as I know, that is impossible.
How can I do then ???