Python 2.7 re.IGNORECASE broken in re.sub?

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


On Sun, 15 Aug 2010 17:36:07 -0700, Alex Willmer wrote:

> 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.

Pardon me, but that's clearly not correct, as proven by the fact that the 
above example works.

You can say that the flags *should* go at the start, for the sake of 
efficiency, or ease of comprehension, or tradition, or to appease the 
Regex Cops who roam the streets beating up those who don't write regexes 
in the approved fashion. But it isn't true that they *must* go at the 
front.


-- 
Steven



More information about the Python-list mailing list