{"id":2833,"library":"types-regex","title":"Typing Stubs for regex","description":"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.","status":"active","version":"2026.4.4.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","regex","typeshed"],"install":[{"cmd":"pip install types-regex regex","lang":"bash","label":"Install stubs and runtime library"}],"dependencies":[{"reason":"This package provides type stubs for the 'regex' runtime library, which must be installed separately to use its functionality.","package":"regex"}],"imports":[{"note":"The 'types-regex' package provides type hints for the 'regex' module; you import 'regex' itself.","symbol":"regex","correct":"import regex"},{"note":"Import specific types like 'Match' directly from the 'regex' module.","symbol":"Match","correct":"from regex import Match"},{"note":"Import specific types like 'Pattern' directly from the 'regex' module.","symbol":"Pattern","correct":"from regex import Pattern"}],"quickstart":{"code":"import regex\nfrom regex import Pattern, Match\n\ndef find_first_email(text: str) -> str | None:\n    # Compile the regex pattern with type annotation\n    email_pattern: Pattern[str] = regex.compile(r\"\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b\")\n    # Search for the pattern in the text\n    match: Match[str] | None = email_pattern.search(text)\n    if match:\n        return match.group(0) # Return the entire matched string\n    return None\n\ntext_data = \"Contact us at info@example.com or support@test.org for assistance.\"\nfirst_email = find_first_email(text_data)\n\nif first_email:\n    print(f\"Found email: {first_email}\")\nelse:\n    print(\"No email found.\")","lang":"python","description":"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."},"warnings":[{"fix":"Always install both packages: `pip install types-regex regex`.","message":"The `types-regex` package only provides type annotations. The actual `regex` runtime library must be installed separately alongside `types-regex` for your code to function at runtime.","severity":"gotcha","affected_versions":"All versions of types-regex"},{"fix":"Refer to the `types-regex` PyPI page for the `regex` version it targets. Consider pinning both `regex` and `types-regex` versions in your `requirements.txt` (e.g., `regex==2026.4.4` and `types-regex==2026.4.4.20260408`) or using flexible version ranges, regularly updating and re-checking types.","message":"The versioning of `types-regex` (e.g., `X.Y.Z.YYYYMMDD`) indicates that it provides stubs for `regex` version `X.Y.Z` and was released on `YYYYMMDD`. Ensure that your installed `types-regex` version is compatible with your `regex` runtime library version to avoid type-checking errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Regularly run your type checker (e.g., MyPy, Pyright) and review any new type errors. If an error is due to an intended API change, update your code. If it seems like an error in the stub, consider contributing a fix to the typeshed project.","message":"Updates to type stubs, even minor ones, can sometimes introduce changes that cause existing code to fail type checking, especially if the underlying library's API has implicit changes not reflected in older stubs, or if stricter types are added.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use raw strings for regex patterns, e.g., `regex.compile(r'\\bword\\b')` instead of `regex.compile('\\bword\\b')`.","message":"When defining regular expression patterns in Python, it's a best practice to use raw string notation (prefixing the string literal with `r`, e.g., `r\"\\d+\"`). This prevents backslashes from being interpreted as Python escape sequences, which can lead to unexpected behavior or `SyntaxWarning` / `SyntaxError` with invalid escape sequences.","severity":"gotcha","affected_versions":"All Python versions using `regex` or `re`"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}