Verbose Mode
re.VERBOSE allows whitespace and comments inside patterns, making complex regex readable.
re.VERBOSE allows whitespace and comments inside patterns, making complex regex readable.
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
Verbose mode is essential for documenting complex patterns — use it freely.