Possible re bug when using ".*"

Ethan Furman ethan at stoneleaf.us
Wed Dec 28 15:37:20 EST 2022


On 12/28/22 11:07, MRAB wrote:
> On 2022-12-28 18:42, Alexander Richert - NOAA Affiliate via Python-list wrote:
>>   In a couple recent versions of Python (including 3.8 and 3.10), the
>> following code:
>> import re
>> print(re.sub(".*", "replacement", "pattern"))
>> yields the output "replacementreplacement".
>>
>> This behavior does not occur in 3.6.
>>
>> Which behavior is the desired one? Perhaps relatedly, I noticed that even
>> in 3.6, the code
>> print(re.findall(".*","pattern"))
>> yields ['pattern',''] which is not what I was expecting.
>>
> It's not a bug, it's a change in behaviour to bring it more into line with other regex implementations in other languages.

The new behavior makes no sense to me, but better to be consistent with the other regex engines than not -- I still get 
thrown off by vim's regex.

--
~Ethan~


More information about the Python-list mailing list