Python Tip #91 (of 365): Use re.VERBOSE to make your regular expressions more re...

@treyhunner@mastodon.social

Python Tip #91 (of 365):

Use re.VERBOSE to make your regular expressions more readable

Regexes are extremely dense: one character is often a whole expression, and they're usually written without whitespace or comments:

match = re.‍search(r"^\d{5}(-\d{4})?$", zip_code)

But they don't have to be so dense!

You can use the re.VERBOSE flag to space them out:

match = re.‍search(r"""
^
\d{5}
( - \d{4} )?
$
""", zip_code, flags=re.VERBOSE)

#Python #DailyPythonTip

🧵(1/4)