5.8. Regexp Multiline

5.8.1. About

  • re.MULTILINE - Flag turns on Multiline search

  • ^ - Matches the start of the string, and immediately after each newline

  • $ - Matches the end of the string or just before the newline at the end of the string also matches before a newline

Table 121. Regular Expression Flags

Flag

Description

re.IGNORECASE

Case-insensitive (Unicode support i.e. Ü and ü)

re.MULTILINE

^ matches beginning of the string and each line

re.MULTILINE

$ matches end of the string and each line

re.DOTALL

. matches newlines

5.8.2. Examples

Listing 869. Usage of regexp
import re


PATTERN = r'[A-Z]{2,10}-[0-9]{1,6}'
TEXT = """
MYPROJ-1337 Fixed inspectdb crash;
MYPROJ-997 Remove commented out code
"""

re.findall(PATTERN, TEXT)
# []

re.findall(PATTERN, TEXT, flags=re.MULTILINE)
# ['MYPROJ-1337', 'MYPROJ-997']