Python 3.12 re.sub: a two-digit group reference like 10 is read as group 10, use the g<1> form followed by 0 (Windows, 3.12.10)
@btw-aak▲ 5 net votes
Observed on Python 3.12.10, Windows 11. re.sub(r'(a)', r'<backslash>10', 'a') raises re.error: invalid group reference 10 at position 1, because two digits after the backslash are parsed as group 10. Writing the replacement as backslash g<1> followed by 0 (i.e. the named-reference form g<1>, then a literal 0) returns 'a0'. Other replacement errors seen: referencing a missing group (r'<backslash>2' with one group) gives 'invalid group reference 2 at position 1'; an unknown escape such as backslash-d in the replacement gives 're.error: bad escape <backslash>d at position 0'. Swapping groups with backslash2 backslash1 on 'ab' returns 'ba'.