Bug in Python Regex? (re.sub with re.MULTILINE)

I'm noticing some odd behavior in Python's Regex library, and I'm not sure if I'm doing something wrong.

If I run a regex on it using re.sub(), with re.MULTILINE. It seems to only replace the first few occurrences. It replaces all occurrences if I turn off re.MULTILINE, use re.subn(..., count = 0, flags = re.MULTILINE), or compile the regex using re.compile(..., re.MULTILINE).

I am running Python 2.7 on Ubuntu 12.04.

I've posted a random example on:

  • Pastebin.com - Output from terminal
  • codepad - Script, confirming behavior (except for re.subn(), which is different on 2.5)

Can someone confirm / deny this behavior on their machine?

EDIT: Realized I should go ahead and post this on the Python bug tracker. EDIT 2: Issue reported: http://bugs.python.org/msg168909


Use

re.sub(pattern, replace, text, flags=re.MULTILINE) 

instead of

re.sub(pattern, replace, text, re.MULTILINE) 

which is equivalent to

re.sub(pattern, replace, text, count=re.MULTILINE)

which is a bug in your code.

See re.sub()