On this page
  1. sre_constants
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Catching Regex Compilation Errors
  7. Gotchas & Boundaries
  8. Classes & Objects
  9. Constants & Attributes

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

Classes & Objects

Constants & Attributes

Linked from

Related notes