On this page
sre_compile
Prev: sqlite3 | Up: stdlib_module | Next: sre_constants
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — sre_compile
Tags: #public #python3 #stdlib
Import
import sre_compile
Overview
Internal regular-expression bytecode compiler formerly backing the re module (deprecated since Python 3.11 in favor of the private re._compiler module). Never import sre_compile in new code—always go through re (re.compile()).
Quick Start
import re
# Public API (re.compile) delegates to the internal regex compiler
pattern = re.compile(r"(?P<word>\w+)=(\d+)")
m = pattern.match("score=42")
assert m is not None and m.group("word") == "score"
Implementation Walkthrough
1. Inspecting Compiled Regex Bytecode Safely
Instead of importing sre_compile, pass re.DEBUG to re.compile() when you want to see the parsed tree and compiled opcode structure of a regular expression:
import contextlib
import io
import re
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
re.compile(r"[a-z]+\d?", re.DEBUG)
assert "MAX_REPEAT" in buf.getvalue()
Gotchas & Boundaries
- Deprecated internal shim: Importing
sre_compileemits aDeprecationWarningand is scheduled for removal; migrate any legacy references to public re APIs. - Unstable bytecode format: Internal opcodes and compiler signatures change freely between CPython releases.
Common Functions
- sre_compile.compile() —
compile(p, flags=0)—sre_compile.compile()function. - sre_compile.dis() —
dis(code)—sre_compile.dis()function. - sre_compile.isstring() —
isstring(obj)—sre_compile.isstring()function.
Classes & Objects
- sre_compile.error — Exception raised for invalid regular expressions. (Methods:
.add_note(),.with_traceback()) - sre_compile.PatternError — Exception raised for invalid regular expressions. (Methods:
.add_note(),.with_traceback())
Constants & Attributes
sre_compile.ANY(_NamedIntConstant) =ANYsre_compile.ANY_ALL(_NamedIntConstant) =ANY_ALLsre_compile.ASSERT(_NamedIntConstant) =ASSERTsre_compile.ASSERT_NOT(_NamedIntConstant) =ASSERT_NOTsre_compile.AT(_NamedIntConstant) =ATsre_compile.ATCODES(list) =[AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT...sre_compile.ATOMIC_GROUP(_NamedIntConstant) =ATOMIC_GROUPsre_compile.AT_BEGINNING(_NamedIntConstant) =AT_BEGINNINGsre_compile.AT_BEGINNING_LINE(_NamedIntConstant) =AT_BEGINNING_LINEsre_compile.AT_BEGINNING_STRING(_NamedIntConstant) =AT_BEGINNING_STRINGsre_compile.AT_BOUNDARY(_NamedIntConstant) =AT_BOUNDARYsre_compile.AT_END(_NamedIntConstant) =AT_ENDsre_compile.AT_END_LINE(_NamedIntConstant) =AT_END_LINEsre_compile.AT_END_STRING(_NamedIntConstant) =AT_END_STRINGsre_compile.AT_LOCALE(dict) ={AT_BOUNDARY: AT_LOC_BOUNDARY, AT_NON_BOUNDARY: AT_LOC_NO...
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/sqlite3
- tuts/python3/stdlib_module/sre_compile/compile
- tuts/python3/stdlib_module/sre_compile/dis
- tuts/python3/stdlib_module/sre_compile/error
- tuts/python3/stdlib_module/sre_compile/isstring
- tuts/python3/stdlib_module/sre_compile/PatternError
- tuts/python3/stdlib_module/sre_constants
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/sqlite3 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_compile/compile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_compile/dis Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_compile/error Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_compile/isstring Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_compile/PatternError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_constants Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib