1

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.

Pavel Francírek
  • 188
  • 2
  • 10

1 Answers1

1

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

Dimitrius
  • 564
  • 6
  • 21