Verbose Flag in Python Regex

A verbose flag provided by the Python’s re module allows you to make your regular expression code more readable and organized because it allows for comments and multiple-line formatting; this is necessary for complex regex patterns.

Syntax:

To enable verbose mode, you add the re.VERBOSE (or re.X) flag when compiling the regex.

import re

pattern = re.compile(r"your_pattern", re.VERBOSE)

Alternatively, you can pass re.VERBOSE as an argument in functions like re.search, re.match, etc.

re.search(r"your_pattern", text, re.VERBOSE)

Features of re.VERBOSE

1. Ignore Whitespace (Except in Character Classes and Escaped Spaces)

  • You may split your regex pattern over more than one line.
  • Spaces and newlines are ignored unless inside brackets ([...]) or escaped (\ ).

2. Allow Comments using #

  • Everything after # is ignored to the end of the line.
  • It makes explaining parts of the regex pattern easier.

1. Using Regex Without re.VERBOSE

A standard regex pattern to match a Social Security Number (SSN) format (123-45-6789) looks like this:

import re

pattern = r"\d{3}-\d{2}-\d{4}"  # Matches a format like "123-45-6789"
match = re.match(pattern, "123-45-6789")

if match:
    print("Valid format")
else:
    print("Invalid format")

Output:

Valid format

Problem: The regex pattern is hard to read and understand.

2. Using re.VERBOSE for Readability

To make the regex pattern more readable, we use re.VERBOSE, breaking it into multiple lines and adding comments.

import re

pattern = re.compile(r"""
    \d{3}     # First three digits
    -         # Hyphen separator
    \d{2}     # Two more digits
    -         # Another hyphen separator
    \d{4}     # Last four digits
    """, re.VERBOSE)

match = pattern.match("123-45-6789")

if match:
    print("Valid format")
else:
    print("Invalid format")

Output:

Valid format

Advantage: This version is more readable and easier to maintain.

3. Handling Whitespace in re.VERBOSE

  • In re.VERBOSE mode, spaces and newlines are ignored unless:
    • They are inside character classes [ ... ]
    • They are escaped using \

Example 1: Ignoring Spaces

import re

pattern = re.compile(r"""
    \d{3}   -   \d{2}   -   \d{4}  # Matches "123-45-6789"
    """, re.VERBOSE)

match = pattern.match("123-45-6789")

if match:
    print("Valid format")
else:
    print("Invalid format")

Output:

Valid format

Spaces in the regex pattern do not affect matching.

Example 2: Matching Spaces Explicitly

If we want to match 123 - 45 - 6789 (with spaces), we must escape spaces or use \s:

import re

pattern = re.compile(r"""
    \d{3}   \s*   -   \s*   \d{2}   \s*   -   \s*   \d{4}  
    """, re.VERBOSE)

match = pattern.match("123 - 45 - 6789")

if match:
    print("Valid format")
else:
    print("Invalid format")

Output:

Valid format

\s* ensures that spaces around hyphens are optional.

4. Combining re.VERBOSE with other flags

You can combine several flags using | (bitwise OR).

For example, let’s match an email address, ignoring case (re.IGNORECASE) and using re.VERBOSE for clarity.

import re

pattern = re.compile(r"""
    ^[a-z0-9._%+-]+  # Username part
    @                # @ symbol
    [a-z0-9.-]+      # Domain part
    \.               # Dot
    [a-z]{2,}$       # TLD (like .com, .org)
    """, re.IGNORECASE | re.VERBOSE)

match = pattern.match("Example@domain.COM")

if match:
    print("Valid email")
else:
    print("Invalid email")

Output:

Valid email

Benefits:

  • re.IGNORECASE ensures case-insensitive matching.
  • re.VERBOSE makes the pattern easy to understand.

Summary

  • re.VERBOSE is a multiline regex for better readability.
  • Whitespace is ignored (unless inside [...] or escaped).
  • Comments can be added using #.
  • Can be combined with other flags like re.IGNORECASE.