On this page
  1. sre_parse
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Replacing Common sre_parse Introspection
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects
  10. Constants & Attributes

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

Common Functions

Classes & Objects

Constants & Attributes

Linked from

Related notes