This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python 2.7.8, 2.7.9 re.MULTILINE failure
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Stephen.Evans, ezio.melotti, mrabarnett, r.david.murray
Priority: normal Keywords:

Created on 2015-04-28 19:25 by Stephen.Evans, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
refail.py Stephen.Evans, 2015-04-28 19:25 re.MULTILINE failure example
Messages (4)
msg242205 - (view) Author: Stephen Evans (Stephen.Evans) Date: 2015-04-28 19:25
A simple multiline regex fails when just the re.MULTILINE argument is used, but works when equivalent alternative methods are used. This was tested on Python2.7.8 on FreeBSD and Win32 Python2.7.9

data = re.sub(r'#.*', '', text, re.MULTILINE) # fails

data = re.sub(r'(?m)#.*', '', text) # Ok
data = re.sub(r'#.*', '', text, re.MULTILINE|re.DEBUG) # Ok

All the expressions work correctly with Win64 Python3.4.3
The attached file has the code and with a sample of text that fails.
msg242206 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2015-04-28 19:38
The 4th argument of re.sub is the maximum count (0 means unlimited).

>>> help(re.sub)
Help on function sub in module re:

sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the match object and must return
    a replacement string to be used.

The value of re.MULTILINE is 8:

>>> re.MULTILINE
8

therefore it'll perform a maximum of 8 substitutions.

In summary: not a bug.
msg242363 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-02 01:20
It is, however, frequently reported as a bug, if that makes you feel any better :)
msg257140 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2015-12-28 22:21
See #11957.
History
Date User Action Args
2022-04-11 14:58:16adminsetgithub: 68259
2015-12-28 22:21:48ezio.melottisetmessages: + msg257140
2015-05-02 01:20:27r.david.murraysetnosy: + r.david.murray
messages: + msg242363
2015-04-28 20:04:13serhiy.storchakasetstatus: open -> closed
resolution: not a bug
stage: resolved
2015-04-28 19:38:44mrabarnettsetmessages: + msg242206
2015-04-28 19:25:28Stephen.Evanscreate