How to match pattern over multiple lines in Python?

A regular expression is a group of characters that allows you to use a search pattern to find a string or a set of strings. RegEx is another name for regular expressions.

The re module in Python is used to work with regular expressions. To match the pattern over multiple lines in python by using regular expression, we use <p>.*</p> regular expression.


  • <p>, </p> indicates paragraph tag in HTML5.

  • Implies zero or more occurrence of characters.

  • . is a special character that matches all characters, including newline characters with the help of re.DOTALL flag.

Generally, in regular expression search ‘.’ special character does not match newline characters. To fix this issue, ‘re’ packages provides few predefined flags which modifies how the special characters behave. So, by using re.DOTALL flag, we can match patterns with multiple lines.


In the following example code, we match the span of paragraph by using a regular expression. We begin by importing regular expression module.

import re

Then, we have used search() function which is imported from re module. This function searches the string/paragraph for a match and returns a match object if there is any match. The group() method is used to return the part of the string that is matched.

import re paragraph = \ ''' <p> Tutorials point is a website. It is a platform to enhance your skills. </p> ''' match ='<p>.*</p>', paragraph, re.DOTALL) print(


The following output is obtained on executing the above program.

Tutorials point is a website.
It is a platform to enhance your skills.

Updated on: 08-Nov-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started