{"id":1499,"library":"google-re2","title":"Google RE2 Python Bindings","description":"google-re2 provides Python bindings for Google's high-performance RE2 C++ regular expression library. It offers a fast and secure alternative to Python's built-in `re` module, designed for linear-time execution, but with a more restricted feature set. The library maintains a frequent release cadence, often monthly, reflecting updates to the underlying C++ library.","status":"active","version":"1.1.20251105","language":"en","source_language":"en","source_url":"https://github.com/google/re2","tags":["regex","regular expressions","performance","re2","google"],"install":[{"cmd":"pip install google-re2","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The PyPI package `google-re2` exposes its functionality via the `re2` module name.","wrong":"import google_re2","symbol":"re2","correct":"import re2"}],"quickstart":{"code":"import re2\n\n# Compile a regex pattern\npattern = re2.compile(r'hello (\\w+)')\n\n# Search for a match\nmatch = pattern.search('hello world')\nif match:\n    print(f\"Found: {match.group(0)}\")\n    print(f\"Captured: {match.group(1)}\")\n\n# Find all occurrences\ntext = 'hello python, hello universe'\nall_matches = re2.findall(r'hello (\\w+)', text)\nprint(f\"All matches: {all_matches}\")\n\n# Replace text\nreplaced_text = re2.sub(r'hello', 'hi', text)\nprint(f\"Replaced text: {replaced_text}\")","lang":"python","description":"This example demonstrates basic usage of the `re2` module, including compiling patterns, searching, finding all matches, and performing substitutions. The API largely mirrors Python's built-in `re` module for common operations."},"warnings":[{"fix":"Review and rewrite regex patterns to avoid unsupported features. Consult RE2's C++ syntax documentation for supported features. For patterns requiring full PCRE compatibility, consider `re` or `regex` libraries.","message":"`re2` is NOT a full drop-in replacement for Python's `re` module. It intentionally omits advanced features like backreferences, lookarounds, and some complex Unicode properties for performance and security reasons. Patterns using these features will raise `re2.error: invalid perl_re`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your environment has a C++ compiler and the necessary RE2 C++ development files. On Debian/Ubuntu: `sudo apt-get install build-essential libre2-dev`. On Fedora: `sudo dnf install @development-tools re2-devel`. On Windows, install Visual C++ Build Tools.","message":"Installation may require a C++ compiler (e.g., GCC, Clang, MSVC) and RE2 C++ library headers if pre-built wheels are not available for your specific Python version and operating system. This can complicate deployment in environments without build tools.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Thoroughly test patterns with diverse Unicode inputs. Explicitly use Unicode character properties (e.g., `\\p{L}` for letters) where supported if fine-grained Unicode control is needed.","message":"While `re2` supports UTF-8, its interpretation of certain character classes (e.g., `\\b` for word boundaries, `\\w` for word characters) can differ from Python's `re` module, especially with non-ASCII or complex Unicode text. This can lead to unexpected matching behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}