Python Glossary Русский

Python 3.13+Exceptionsexception

re.PatternError

module exceptions

Exception raised for invalid regular expressions

Syntax

raise re.PatternError

It fires while the pattern is being compiled, not while matching, so it shows up on the very first re.compile or re.search — typically from an unclosed bracket, a dangling quantifier, or user text spliced into a pattern without re.escape(). The msg, pattern and pos attributes tell you exactly which character of the pattern broke. The name re.PatternError only exists from Python 3.13 onward; earlier versions have re.error, which is still a valid alias today.

Examples

import re
try:
    re.compile('[')
except re.PatternError as e:
    print(type(e).__name__)   # → PatternError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy