Regular Expression Modifiers in Python

Regular expression modifiers (also called flags) are optional parameters that control how pattern matching behaves. You can combine multiple modifiers using the bitwise OR operator (|) to customize the search behavior.

Common Regular Expression Modifiers

Modifier Name Description
re.I IGNORECASE Performs case-insensitive matching
re.M MULTILINE Makes ^ and $ match start/end of each line
re.S DOTALL Makes dot (.) match any character including newline
re.X VERBOSE Ignores whitespace and allows comments in patterns
re.L LOCALE Uses locale-aware matching (deprecated)
re.U UNICODE Uses Unicode character properties (Python 2 only)

Case-Insensitive Matching (re.I)

The re.I flag makes pattern matching ignore case differences ?

import re

text = "Hello World"
pattern = "hello"

# Without re.I flag
result1 = re.search(pattern, text)
print("Without re.I:", result1)

# With re.I flag
result2 = re.search(pattern, text, re.I)
print("With re.I:", result2.group() if result2 else "No match")
Without re.I: None
With re.I: Hello

Multiline Mode (re.M)

The re.M flag changes how ^ and $ behave with multiline strings ?

import re

text = "First line\nSecond line\nThird line"

# Without re.M - ^ only matches start of string
matches1 = re.findall(r"^\w+", text)
print("Without re.M:", matches1)

# With re.M - ^ matches start of each line
matches2 = re.findall(r"^\w+", text, re.M)
print("With re.M:", matches2)
Without re.M: ['First']
With re.M: ['First', 'Second', 'Third']

Dot Matches All (re.S)

By default, dot (.) doesn't match newline characters. The re.S flag changes this behavior ?

import re

text = "Line1\nLine2"

# Without re.S - dot doesn't match newline
match1 = re.search(r"Line1.Line2", text)
print("Without re.S:", match1)

# With re.S - dot matches newline too
match2 = re.search(r"Line1.Line2", text, re.S)
print("With re.S:", match2.group() if match2 else "No match")
Without re.S: None
With re.S: Line1
Line2

Combining Multiple Flags

You can combine flags using the bitwise OR operator (|) ?

import re

text = "FIRST line\nsecond LINE"

# Combine case-insensitive and multiline flags
matches = re.findall(r"^.+line", text, re.I | re.M)
print("Combined flags:", matches)
Combined flags: ['FIRST line', 'second LINE']

Verbose Mode (re.X)

The re.X flag allows you to write more readable patterns with whitespace and comments ?

import re

text = "Contact: john@example.com"

# Verbose pattern with comments
pattern = r"""
    \w+         # Username part
    @           # At symbol
    \w+         # Domain name
    \.          # Dot
    \w{2,3}     # Domain extension
"""

match = re.search(pattern, text, re.X)
print("Email found:", match.group() if match else "No match")
Email found: john@example.com

Conclusion

Regular expression modifiers provide powerful control over pattern matching behavior. The most commonly used flags are re.I for case-insensitive matching, re.M for multiline mode, and re.S for dot-all matching. Combine multiple flags with | to customize matching behavior for complex text processing tasks.

Updated on: 2026-03-25T07:42:19+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements