re.MULTILINE

Full name
re.MULTILINE
Library
re
Syntax

re.MULTILINE

Description

The re.MULTILINE search modifier forces the ^ symbol to match at the beginning of each line of text (and not just the first), and the $ symbol to match at the end of each line of text (and not just the last one).

Parameters

The re.MULTILINE search modifier takes no arguments.

Examples

In this example we start with the following text and search pattern:

text = "dogs and cats play\ndogs and cats eat"
pattern = r"^dog"

Note that the text includes a line break:

print(text)
dogs and cats play
dogs and cats eat

By default, the regex search will only find a match at the beginning of the text (on the first line):

re.findall(pattern, text)
['dog']

However, using the re.MULTILINE modifier will cause matches to be found at the beginning of each line:

re.findall(pattern, text, flags = re.MULTILINE)
['dog', 'dog']
Submitted by admin on Wed, 05/19/2021 - 09:07