Skip to content

sre_parse Module

The sre_parse module parses regular expression patterns into an abstract syntax tree used by the regex engine.

Complexity Reference

Operation Time Space Notes
Parse pattern O(n) O(n) n = pattern length
Build AST O(n) O(n) n = pattern complexity

Parsing Regex Patterns

Analyzing Regex Structure

import sre_parse

# Parse pattern - O(n)
pattern = r'\d+@[\w.-]+\.\w+'
ast = sre_parse.parse(pattern)

# Examine structure - O(1)
print(ast)
# Shows the parse tree

# Pattern analysis
for item in ast:
    print(f"Token: {item}")