Python 2.7 re.IGNORECASE broken in re.sub?

Steven D'Aprano steve at
Tue Aug 17 01:10:41 CEST 2010

On Mon, 16 Aug 2010 05:46:17 -0700, Alex Willmer wrote:

> On Aug 16, 12:23 pm, Steven D'Aprano <st... at REMOVE-THIS-
>> wrote:
>> 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-
>> >> 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.
> Undefined includes 'might work sometimes'. I refer you to the Python
> documentation:
> "Note that the (?x) flag changes how the expression is parsed. It should
> be used first in the expression string, or after one or more whitespace
> characters. If there are non-whitespace characters before the flag, the
> results are undefined."

Well so it does. I stand corrected.

I note though that even the docs say "should" rather than "must". I 
wonder whether the documentation author is just being cautious, because 
I've seen comments on the python-dev list that imply that the current 
behaviour of flags (that their effect is global to the regex) is 
supported. E.g.:

At the point that people are seriously considering changing the behaviour 
of a replacement re engine in order to support the current "undefined" 
behaviour, perhaps that behaviour isn't quite so undefined and the docs 
need to be re-written?


More information about the Python-list mailing list