Alex Rivera | Logout

Python re.sub with a flag does not replace all occurrences

Asked 2008-09-03T21:00:33.863
78

The Python docs say:

re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...

So what's going on when I get the following unexpected result?

>>> import re
>>> s = """// The quick brown fox.
... // Jumped over the lazy dog."""
>>> re.sub('^//', '', s, re.MULTILINE)
' The quick brown fox.\n// Jumped over the lazy dog.'
Edit
Report

1 Answer

12
re.sub('(?m)^//', '', s)
answered 2010-03-25T16:10:45.187

Your Answer