backrefs is a Python library that extends the functionality of the standard `re` module and the third-party `regex` module by adding additional back references. It introduces features like `\c` for character class back-references, `\k<name>` for named capture groups that act as character classes, and enhanced atomic grouping. The library maintains an active development status, with regular minor releases addressing new features, bug fixes, and Python version compatibility.
pip install backrefsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `backrefs.bre` to leverage character class back-references (e.g., `\c1` or `\k<name>`), a powerful feature not available in Python's standard `re` module. The example shows searching for repeating words and replacing them.
To explicitly use standard Unicode rules for affected properties, use their Unicode property form instead (e.g., `[\p{Alnum}]` instead of `[[:alnum:]]`). Review and test existing regex patterns carefully after upgrading.Upgrade your Python environment to 3.9 or newer to use backrefs 5.8+.
Choose the appropriate module (`bre` or `bregex`) based on your needs and installed dependencies. If you need features from the `regex` library (e.g., fuzzy matching, recursion), use `bregex` and ensure `regex` is installed.
This issue was fixed in version 6.0.1. Users on 6.0 should upgrade to 6.0.1 or newer to avoid this specific regression.
To match a literal `\c`, escape the backslash: use `\\c`. If you intended to match a control character (e.g., `\cA`), Python's `re` module does not support this syntax; you must use the actual character value (e.g., `\x01` for Ctrl+A). Note that `backrefs` *does* interpret `\c<name>` as a named backreference.
Do not use `\c` in patterns with `backrefs.bre`. If you need to match a literal 'c', use 'c'. If you intended a control character, use the appropriate hexadecimal (`\xNN`) or Unicode (`\uNNNN`) escape, or the character directly.
Use `from backrefs import bre` to import the `backrefs` wrapper for the `re` module, or `from backrefs import bref` for the `regex` module wrapper.
Review the regular expression pattern to ensure all backreferences correctly correspond to an existing capturing group. For named backreferences (`\k<name>`), verify that the named group has been defined (e.g., `(?P<name>...)`). Always use raw strings (e.g., `r'pattern'`) for regular expressions to avoid issues with Python's string literal escaping.
Install the `backrefs` library using pip: `pip install backrefs`.