Python 2.7 re.IGNORECASE broken in re.sub?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Aug 16 02:07:53 CEST 2010


On Sun, 15 Aug 2010 16:45:49 -0700, Christopher wrote:

> I have the following problem:
>
>>>> t="Python26"
>>>> import re
>>>> re.sub(r"python\d\d", "Python27", t)
> 'Python26'
>>>> re.sub(r"python\d\d", "Python27", t, re.IGNORECASE)
> 'Python26'
>>>> re.sub(r"Python\d\d", "Python27", t, re.IGNORECASE)
> 'Python27'

> Is this a known bug?  Is it by design for some odd reason?


>>> help(re.sub)

Help on function sub in module re:

    sub(pattern, repl, string, count=0)
    ...


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'



-- 
Steven



More information about the Python-list mailing list