Difference in re.sub in Python between version 3.6 and 3.10

Question:

I just found strange (for me) difference in regular expression module in Python3. Is it some change between version 3.6.9 and 3.10.6 that I overlooked? In fact, it looks like regression to me.

Code:

import re
RE_IP = re.compile(r'[0-9]*$')
RE_IP.sub('0', '1.2.3.4')

result in Python 3.10.6 is '1.2.3.00' and in Python 3.6.9: '1.2.3.0'
The latter result is what I expect.

Asked By: Pavel Francírek

||

Answers:

The problem is that your regular exception matches empty string. You can make it and it will work fine. I’ll try to find documentation changes and update my answer.

import re
RE_IP = re.compile(r'[0-9]+$')
RE_IP.sub('0', '1.2.3.4')

UPD:

According to the comments by @j1-lee

This answer and this change
"Changed in version 3.7: Empty matches for the pattern are replaced when adjacent to a previous non-empty match." explains the changes

Answered By: Dimitrius
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.