Is this a bug in re.sub with IGNORECASE?

Is this a bug in re.sub?

Python 3.11.7 (main, Dec  4 2023, 18:10:11) [GCC 13.2.0] on linux
>>> import re
>>> re.sub(r"[^a-z0-9\.]+", ".", "..-asdf///.as.///", re.IGNORECASE)
'...asdf..as.///'
>>> re.sub(r"[^a-z0-9\.]+", ".", "..-asdf///.as.///")
'...asdf..as..'

Note how the last "///" gets replaced without IGNORECASE but doesn’t if it’s passed.

When I try this on a newer Python, I get a very useful clue:

>>> re.sub(r"[^a-z0-9\.]+", ".", "..-asdf///.as.///", re.IGNORECASE)
<python-input-5>:1: DeprecationWarning: 'count' is passed as positional argument
  re.sub(r"[^a-z0-9\.]+", ".", "..-asdf///.as.///", re.IGNORECASE)
'...asdf..as.///'

Hint: re.IGNORECASE has the value of 2.

4 Likes

:bulb:
aaah! I need flags = re.IGNORECASE, because I was effectively passing count = 2!

1 Like