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

PythonRegex

Python Problem Overview


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.'

Python Solutions


Solution 1 - Python

Look at the definition of re.sub:

re.sub(pattern, repl, string[, count, flags])

The 4th argument is the count, you are using re.MULTILINE (which is 8) as the count, not as a flag.

Either use a named argument:

re.sub('^//', '', s, flags=re.MULTILINE)

Or compile the regex first:

re.sub(re.compile('^//', re.MULTILINE), '', s)

Solution 2 - Python

re.sub('(?m)^//', '', s)

Solution 3 - Python

The full definition of re.sub is:

re.sub(pattern, repl, string[, count, flags])

Which means that if you tell Python what the parameters are, then you can pass flags without passing count:

re.sub('^//', '', s, flags=re.MULTILINE)

or, more concisely:

re.sub('^//', '', s, flags=re.M)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncdlearyView Question on Stackoverflow
Solution 1 - PythonMoeView Answer on Stackoverflow
Solution 2 - PythonFerranView Answer on Stackoverflow
Solution 3 - PythonpseudosudoView Answer on Stackoverflow