I would like to search and replace a block of text which contains new line characters.
In the example below when the DOTALL flag is specified, findall behaves as expected and '.'
matches any character including a newline.
But when calling sub, the DOTALL flag doesn't seem to do anything and no matches are found. I just want to confirm that I can't use '.' with sub to replace text that contains new line characters or if I'm not calling the function correctly.
Code
import re
text = """
some example text...
START
bla bla
bla bla
END
"""
print 'this works:', re.findall('START.*END', text, re.DOTALL)
print 'this fails:', re.sub('START.*END', 'NEWTEXT', text, re.DOTALL)
Output
this works: ['START\nbla bla\nbla bla\nEND']
this fails:
some example text...
START
bla bla
bla bla
END