On this page
sre_parse
Prev: sre_constants | Up: stdlib_module | Next: ssl
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — sre_parse
Tags: #public #python3 #stdlib
Import
import sre_parse
Overview
Internal regular-expression syntax tree parser formerly backing re (deprecated since Python 3.11 in favor of re._parser). Avoid importing sre_parse in application code; use re (and re.DEBUG for diagnostics) or a dedicated third-party regex-AST library if you need to analyze or generate regular expressions.
Quick Start
import re
# Public re.Pattern exposes group counts and named group mappings directly
pat = re.compile(r"(?P<year>\d{4})-(?P<month>\d{2})")
assert pat.groups == 2
assert pat.groupindex == {"year": 1, "month": 2}
Implementation Walkthrough
1. Replacing Common sre_parse Introspection
Most legacy uses of sre_parse.parse() only needed group names, flags, or pattern debugging—all available on public re.Pattern attributes (pat.pattern, pat.flags, pat.groups, pat.groupindex) without touching private internals.
Gotchas & Boundaries
- Deprecated internal shim: Importing
sre_parseemits aDeprecationWarningand is not part of Python's public API. - Private AST node layout: The tuple/SubPattern structure returned by the internal parser changes across Python versions.
Common Functions
- sre_parse.fix_flags() —
fix_flags(src, flags)—sre_parse.fix_flags()function. - sre_parse.parse() —
parse(str, flags=0, state=None)—sre_parse.parse()function. - sre_parse.parse_template() —
parse_template(source, pattern)—sre_parse.parse_template()function.
Classes & Objects
- sre_parse.error — Exception raised for invalid regular expressions. (Methods:
.add_note(),.with_traceback()) - sre_parse.PatternError — Exception raised for invalid regular expressions. (Methods:
.add_note(),.with_traceback()) - sre_parse.State —
sre_parse.Stateclass. (Methods:.checkgroup(),.checklookbehindgroup(),.closegroup(),.groups(),.opengroup()) - sre_parse.SubPattern —
sre_parse.SubPatternclass. (Methods:.append(),.dump(),.getwidth(),.insert()) - sre_parse.Tokenizer —
sre_parse.Tokenizerclass. (Methods:.checkgroupname(),.error(),.get(),.getuntil(),.getwhile(),.match())
Constants & Attributes
sre_parse.ANY(_NamedIntConstant) =ANYsre_parse.ANY_ALL(_NamedIntConstant) =ANY_ALLsre_parse.ASCIILETTERS(frozenset) =frozenset({'d', 'V', 'M', 'v', 'j', 'Q', 'i', 'r', 'Y', '...sre_parse.ASSERT(_NamedIntConstant) =ASSERTsre_parse.ASSERT_NOT(_NamedIntConstant) =ASSERT_NOTsre_parse.AT(_NamedIntConstant) =ATsre_parse.ATCODES(list) =[AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT...sre_parse.ATOMIC_GROUP(_NamedIntConstant) =ATOMIC_GROUPsre_parse.AT_BEGINNING(_NamedIntConstant) =AT_BEGINNINGsre_parse.AT_BEGINNING_LINE(_NamedIntConstant) =AT_BEGINNING_LINEsre_parse.AT_BEGINNING_STRING(_NamedIntConstant) =AT_BEGINNING_STRINGsre_parse.AT_BOUNDARY(_NamedIntConstant) =AT_BOUNDARYsre_parse.AT_END(_NamedIntConstant) =AT_ENDsre_parse.AT_END_LINE(_NamedIntConstant) =AT_END_LINEsre_parse.AT_END_STRING(_NamedIntConstant) =AT_END_STRING
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/sre_constants
- tuts/python3/stdlib_module/sre_parse/error
- tuts/python3/stdlib_module/sre_parse/fix_flags
- tuts/python3/stdlib_module/sre_parse/parse
- tuts/python3/stdlib_module/sre_parse/parse_template
- tuts/python3/stdlib_module/sre_parse/PatternError
- tuts/python3/stdlib_module/sre_parse/State
- tuts/python3/stdlib_module/sre_parse/SubPattern
- tuts/python3/stdlib_module/sre_parse/Tokenizer
- tuts/python3/stdlib_module/ssl
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/sre_constants Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse/error Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse/fix_flags Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse/parse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse/parse_template Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse/PatternError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse/State Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse/SubPattern Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse/Tokenizer Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ssl Linked here; links to this page; shared tags: #stdlib