On this page
sre_constants
Prev: sre_compile | Up: stdlib_module | Next: sre_parse
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org โ sre_constants
Tags: #public #python3 #stdlib
Import
import sre_constants
Overview
Internal regular-expression opcode and flag constants module formerly backing re (deprecated since Python 3.11 in favor of re._constants). Use the public constants exposed directly on re (re.IGNORECASE, re.MULTILINE, re.RegexFlag, re.error) instead of importing sre_constants.
Quick Start
import re
# Use public re flags and re.error rather than sre_constants
flags = re.IGNORECASE | re.MULTILINE
assert re.search(r"^hello$", "HELLO", flags) is not None
Implementation Walkthrough
1. Catching Regex Compilation Errors
Legacy code occasionally caught sre_constants.error; replace it with re.error (or re.PatternError in Python 3.13+):
import re
try:
re.compile(r"[unclosed")
except re.error as exc:
assert "unterminated" in str(exc)
Gotchas & Boundaries
- Deprecated internal shim: Importing
sre_constantsraises aDeprecationWarningin modern Python 3. - No stability guarantee: Opcode values (
LITERAL,BRANCH,MAX_REPEAT, etc.) are CPython implementation details.
Classes & Objects
- sre_constants.error โ Exception raised for invalid regular expressions. (Methods:
.add_note(),.with_traceback()) - sre_constants.PatternError โ Exception raised for invalid regular expressions. (Methods:
.add_note(),.with_traceback())
Constants & Attributes
sre_constants.ANY(_NamedIntConstant) =ANYsre_constants.ANY_ALL(_NamedIntConstant) =ANY_ALLsre_constants.ASSERT(_NamedIntConstant) =ASSERTsre_constants.ASSERT_NOT(_NamedIntConstant) =ASSERT_NOTsre_constants.AT(_NamedIntConstant) =ATsre_constants.ATCODES(list) =[AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT...sre_constants.ATOMIC_GROUP(_NamedIntConstant) =ATOMIC_GROUPsre_constants.AT_BEGINNING(_NamedIntConstant) =AT_BEGINNINGsre_constants.AT_BEGINNING_LINE(_NamedIntConstant) =AT_BEGINNING_LINEsre_constants.AT_BEGINNING_STRING(_NamedIntConstant) =AT_BEGINNING_STRINGsre_constants.AT_BOUNDARY(_NamedIntConstant) =AT_BOUNDARYsre_constants.AT_END(_NamedIntConstant) =AT_ENDsre_constants.AT_END_LINE(_NamedIntConstant) =AT_END_LINEsre_constants.AT_END_STRING(_NamedIntConstant) =AT_END_STRINGsre_constants.AT_LOCALE(dict) ={AT_BOUNDARY: AT_LOC_BOUNDARY, AT_NON_BOUNDARY: AT_LOC_NO...
Linked from
Related notes
- tuts/python3/stdlib_module/sre_compile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_constants/error Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_constants/PatternError Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sre_parse 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
- tuts/python3/stdlib_module/dataclasses Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/re Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sys Linked here; shared tags: #stdlib