Python 2.7 re.IGNORECASE broken in re.sub?

Christopher nadiasvertex at gmail.com
Mon Aug 16 07:40:12 EDT 2010


On Aug 15, 8:07 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> 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'
>

Thanks. Somehow I didn't notice that other argument after looking at
it a million times. :-)



More information about the Python-list mailing list