Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading