Python 2.7 re.IGNORECASE broken in re.sub?

MRAB python at mrabarnett.plus.com
Mon Aug 16 03:13:43 CEST 2010


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. Also in Python 2.7 re.sub() has a
> flags argument.
> 
[snip]
In re such flags apply to the entire regex, no matter where they appear.
This even applies to the (?x) (VERBOSE) flag; if re sees it at the end
of the regex then it has to re-scan the entire regex!

For clarity and compatibility with other regex implementations, put it
initially.



More information about the Python-list mailing list