How to match pattern over multiple lines in Python?

PythonServer Side ProgrammingProgramming

The re.DOTALL flag tells python to make the ‘.’ special character match all characters, including newline characters.

import re
paragraph = \
'''
   This is a paragraph.
   It has multiple lines.
'''
match = re.search(r'<p>.*</p>', paragraph, re.DOTALL)
print match.group(0)

Output

This is a paragraph.
It has multiple lines.
raja
Published on 02-Jan-2018 07:19:31
Advertisements