{"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":"python","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,"search_vec":"'altern':28 'bind':4,10 'built':33 'built-in':32 'c':18,64 'cadenc':56 'design':37 'execut':42 'express':20,68 'fast':25 'featur':48 'frequent':54 'googl':1,6,12,71 'google-re2':5 'high':15 'high-perform':14 'librari':21,51,65 'linear':40 'linear-tim':39 'maintain':52 'modul':36 'month':58 'offer':23 'often':57 'perform':16,69 'provid':8 'python':3,9,30 're':35 're2':2,7,17,70 'reflect':59 'regex':66 'regular':19,67 'releas':55 'restrict':47 'secur':27 'set':49 'time':41 'under':63 'updat':60","created_at":"2026-04-09T03:50:34.217941+00:00","updated_at":"2026-04-16T15:25:29.082992+00:00","problems":[{"fix":"Ensure that required system dependencies are installed (e.g., `sudo apt-get install libre2-dev pybind11-dev` on Debian/Ubuntu, or ensure Xcode Command Line Tools are updated on macOS) and install `pybind11` via `pip install pybind11`. Consider using a clean Python virtual environment.","cause":"This error commonly occurs during installation due to missing C++ development tools (like a C++ compiler or the `libre2-dev` package) or `pybind11`, or environment-specific issues, particularly on macOS where Xcode Command Line Tools might be outdated or absent.","error":"ERROR: Failed building wheel for google-re2"},{"fix":"Rewrite the regular expression to avoid unsupported features by using alternative patterns or string manipulation before or after the regex match. For example, `(?:$|[^,])` can often replace `(?!,)`.","cause":"The RE2 library has a more restricted feature set than PCRE or Python's built-in `re` module; it does not support advanced regular expression features like lookarounds (e.g., positive/negative lookahead `(?=...)`, `(?!...)`, or lookbehind `(?<=...)`, `(?<!...)`) or backreferences.","error":"re2.error: invalid perl operator: (?!)"},{"fix":"First, ensure the package is installed using `pip install google-re2`. Then, import it correctly in your Python code using `import re2`.","cause":"This error occurs when the `google-re2` package has not been installed, or it was installed but imported with an incorrect module name. The package is imported as `re2`, not `google_re2` or similar variations.","error":"ModuleNotFoundError: No module named 're2'"},{"fix":"Ensure you have the `google-re2` package (or a Python 3 compatible `pyre2` binding) installed for your current Python 3 environment, and upgrade it to the latest version using `pip install --upgrade google-re2`.","cause":"This error indicates an incompatibility issue, typically arising when an older version of the `re2` Python binding or a binding designed for Python 2 is being used in a Python 3 environment. `basestring` was a built-in type in Python 2 that was removed in Python 3.","error":"NameError: basestring"}],"ecosystem":"pypi","meta_description":null,"install_score":23,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.1.20251105","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/google/re2","docs":null,"changelog":null,"pypi":"https://pypi.org/project/google-re2/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"stale"}}