{"id":1396,"library":"backrefs","title":"backrefs","description":"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.","status":"active","version":"6.2","language":"en","source_language":"en","source_url":"https://github.com/facelessuser/backrefs","tags":["regex","regular expressions","backreferences","re","patterns"],"install":[{"cmd":"pip install backrefs","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required if using `backrefs.bregex` for advanced regex features beyond what `re` offers.","package":"regex","optional":true}],"imports":[{"note":"While 'import backrefs.bre as bre' works, the direct 'from backrefs import bre' is idiomatic and commonly used in examples.","wrong":"import backrefs.bre as bre","symbol":"bre","correct":"from backrefs import bre"},{"note":"Similar to `bre`, direct import is idiomatic. Remember that `bregex` requires the `regex` package to be installed.","wrong":"import backrefs.bregex as bregex","symbol":"bregex","correct":"from backrefs import bregex"}],"quickstart":{"code":"import os\nfrom backrefs import bre\n\n# Example demonstrating character class back-references (\\c)\n# Standard 're' does not support directly referencing a captured group as a character class.\n\ntext = \"apple banana banana orange\"\n# Pattern to match a word followed by a space and then the same word,\n# using \\c1 to reference the first captured group as a character class.\npattern_with_c = r'(\\b\\w+\\b)\\s\\c1'\n\n# Using backrefs.bre (which extends the standard 're' module)\nmatch = bre.search(pattern_with_c, text)\n\nif match:\n    print(f\"Matched: '{match.group(0)}'\")\n    print(f\"First word captured: '{match.group(1)}'\")\nelse:\n    print(\"No match found.\")\n\n# Another example: replacing duplicate consecutive words\ntext_dupe = \"hello hello world world test\"\npattern_dupe = r'(\\b\\w+\\b)\\s\\c1'\n# Replace \"word word\" with just \"word\"\nresult = bre.sub(pattern_dupe, r'\\1', text_dupe)\nprint(f\"Original: '{text_dupe}'\")\nprint(f\"After replacing duplicates: '{result}'\")","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"In version 6.0, the behavior of POSIX character classes (e.g., `[[:alnum:]]`, `[[:digit:]]`) was changed to always use POSIX compatibility rules instead of Unicode standard rules where applicable. This might break existing patterns that relied on the previous Unicode standard behavior for these classes.","severity":"breaking","affected_versions":"6.0 and later"},{"fix":"Upgrade your Python environment to 3.9 or newer to use backrefs 5.8+.","message":"Python 3.8 support was officially dropped in version 5.8. Users on Python 3.8 or older will need to upgrade their Python version or stay on backrefs < 5.8.","severity":"breaking","affected_versions":"5.8 and later"},{"fix":"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.","message":"backrefs provides two main interfaces: `bre` and `bregex`. `bre` wraps Python's built-in `re` module, while `bregex` wraps the third-party `regex` module. `bregex` offers more advanced regex features (inherited from `regex`) but requires `pip install regex`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"A regression in version 6.0 created an ASCII binary property that would override an ASCII block property, leading to incorrect matching behavior in specific scenarios.","severity":"gotcha","affected_versions":"6.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}