PureDevOps Community

Python: How to match a multiline string with regular expression

In this script, we define the match_multiline_string function that takes a regular expression pattern and a multiline string as input. It compiles the pattern using re.compile, specifying the re.MULTILINE and re.DOTALL flags. The re.MULTILINE flag allows the pattern to match at the beginning or end of each line in the multiline string, and the re.DOTALL flag allows the dot (.) in the pattern to match any character, including newline characters.

The function then uses findall to find all non-overlapping occurrences of the pattern in the multiline string. The matches are returned as a list.

In the example usage, we define a multiline string with some sample text. We also define a pattern that matches a sentence starting with “Lorem” and ending with a period. We call the match_multiline_string function with the pattern and the multiline string, and the resulting matches are printed.

Note that you can modify the pattern variable to match your desired pattern using regular expressions.

import re

def match_multiline_string(pattern, text):
    regex = re.compile(pattern, re.MULTILINE | re.DOTALL)
    matches = regex.findall(text)
    return matches

# Example usage
multiline_string = """
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Donec vitae magna eget leo
euismod congue.
"""

pattern = r"(Lorem.*?\.)"  # Example pattern to match a sentence starting with "Lorem" and ending with a period.

matches = match_multiline_string(pattern, multiline_string)
print(matches)