Why does re.sub replace the entire pattern, not just a capturing group within it?
Because it’s supposed to replace the whole occurrence of the pattern: Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. If it were to replace only some subgroup, then complex regexes with several groups wouldn’t work. There are several possible solutions: Specify pattern in full: … Read more