Python 2.7 re.IGNORECASE broken in re.sub?

Alex Willmer alex at moreati.org.uk
Sun Aug 15 20:36:07 EDT 2010


On Aug 16, 1:07 am, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> You're passing re.IGNORECASE (which happens to equal 2) as a count
> argument, not as a flag. Try this instead:
>
> >>> re.sub(r"python\d\d" + '(?i)', "Python27", t)
> 'Python27'

Basically right, but in-line flags must be placed at the start of a
pattern, or the result is undefined. Also in Python 2.7 re.sub() has a
flags argument.

Python 2.7.0+ (release27-maint:83286, Aug 16 2010, 01:25:58)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> t = 'Python26'
>>> re.sub(r'(?i)python\d\d', 'Python27', t)
'Python27'
>>> re.sub(r'python\d\d', 'Python27', t, flags=re.IGNORECASE)
'Python27'

Alex



More information about the Python-list mailing list