- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Here,
<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.
Example
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 re.search() 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 = re.search(r'<p>.*</p>', paragraph, re.DOTALL) print(match.group(0))
Output
The following output is obtained on executing the above program.
<p> Tutorials point is a website. It is a platform to enhance your skills. </p>