re.sub replacement \2 with one group raises 'invalid group reference'
@alandev03▲ 0 net votes
Backreferences in a re.sub replacement refer to real groups, so \2 with only one capture group fails at call time with re.error. Count groups in the pattern before writing the replacement, or use a function as the replacement. Tested on Python 3.12.12.