Thursday 25 April 2013

Python: re.sub does not replace all matches

Trying to replace patterns in a multiline string:

re.sub(pattern, replacement, string, re.M)

This does not replace all matches, because the fourth parameter is the number of matches to be replaced. The fix is

re.sub(pattern, replacement, string, flags=re.M)

No comments:

Post a Comment