SyntaxStudy
Sign Up
Python Verbose Mode for Complex Patterns
Python Intermediate 4 min read

Verbose Mode for Complex Patterns

Verbose Mode

re.VERBOSE allows whitespace and comments inside patterns, making complex regex readable.

Example
import re

email_pattern = re.compile(r"""
    [\w.+-]+        # username
    @               # at sign
    [\w-]+          # domain name
    (?:\.[\w-]+)*   # subdomains
    \.\w{2,4}       # TLD
""", re.VERBOSE)

print(bool(email_pattern.match("user@example.com")))  # True
Pro Tip

Verbose mode is essential for documenting complex patterns — use it freely.