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

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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)

Method 2

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

Method 3

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)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x