Regular Expressions › Flags and Modifiers

Michael BrenndoerferUpdated March 18, 20252 min read
Language AI HandbookAll chapters
Part 1 of 67Text as Data
Chapter 3Regular Expressions › Flags and Modifiers
Reading section 10 of 21 · Flags and Modifiers · 2 minSection 10 of 21 · 2 min

Book contents

Section 10 of 21 · 2 min read

Regular Expressions › Flags and ModifiersOne section · Sections in this chapter10 / 21 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Regex flags modify how patterns are interpreted. They are the equivalent of options or settings that change the rules the engine uses when evaluating a pattern. You can pass flags as the third argument to any re function, or combine multiple flags with the | operator. You can also embed flags directly inside a pattern using inline syntax like (?i) for case-insensitive, which is useful when you are storing patterns as strings and cannot pass them as function arguments.

In[43]:
Code
text = """Hello World
hello python
HELLO REGEX"""

# re.IGNORECASE (re.I) - case-insensitive matching
case_insensitive = re.findall(r"hello", text, re.IGNORECASE)

# re.MULTILINE (re.M) - ^ and $ match line boundaries
multiline = re.findall(r"^hello", text, re.IGNORECASE | re.MULTILINE)

# re.DOTALL (re.S) - dot matches newline
dotall = re.findall(r"Hello.*REGEX", text, re.DOTALL)

# re.VERBOSE (re.X) - allows comments and whitespace in patterns
pattern = re.compile(
    r"""
    \d{4}    # Year
    -        # Separator
    \d{2}    # Month
    -        # Separator
    \d{2}    # Day
""",
    re.VERBOSE,
)
Out[44]:
Console
Text:
Hello World
hello python
HELLO REGEX

re.IGNORECASE:
  Pattern: hello
  Matches: ['Hello', 'hello', 'HELLO']

re.MULTILINE:
  Pattern: ^hello (with IGNORECASE)
  Matches: ['Hello', 'hello', 'HELLO']

re.DOTALL:
  Pattern: Hello.*REGEX
  Matches: ['Hello World\nhello python\nHELLO REGEX']

re.VERBOSE allows readable patterns:
  Pattern matches: 2024-01-15 → True

The re.VERBOSE flag is particularly valuable for complex patterns. It lets you break patterns across lines and add comments, making them maintainable. Consider using re.VERBOSE as the default for any pattern longer than about 20 characters. A pattern that takes five minutes to write but is incomprehensible six months later is not a productivity win. Verbose mode pays for itself on the first re-read.

The re.MULTILINE flag is another one that deserves special attention. Without it, ^ matches only at the very beginning of the entire string and $ matches only at the very end. With re.MULTILINE, they match at the beginning and end of each line within the string. This matters enormously when processing multi-line documents: searching for lines that start with a specific word, finding headings, or validating that each line in a file conforms to a format. If you find that ^ or $ anchors are not working as expected on multi-line text, the first thing to check is whether you need re.MULTILINE.