Typing Stubs for regex

2026.4.4.20260408 · active · verified Fri Apr 10

types-regex provides static type annotations (stubs) for the `regex` package, an alternative regular expression module for Python. It is part of the typeshed project and allows type checkers like Mypy and Pyright to perform static analysis on code using the `regex` library. The current version is 2026.4.4.20260408, and stub packages from typeshed are automatically released, often multiple times a day, to keep up with runtime library changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `regex` library with type hints provided by `types-regex`. It shows compiling a regular expression pattern and then searching for it in a string, ensuring `Pattern` and `Match` objects are correctly typed.

import regex
from regex import Pattern, Match

def find_first_email(text: str) -> str | None:
    # Compile the regex pattern with type annotation
    email_pattern: Pattern[str] = regex.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b")
    # Search for the pattern in the text
    match: Match[str] | None = email_pattern.search(text)
    if match:
        return match.group(0) # Return the entire matched string
    return None

text_data = "Contact us at info@example.com or support@test.org for assistance."
first_email = find_first_email(text_data)

if first_email:
    print(f"Found email: {first_email}")
else:
    print("No email found.")

view raw JSON →