Computer >> Computer tutorials >  >> Programming >> Python

How to match pattern over multiple lines in Python?


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.