backrefs

6.2 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

import os
from backrefs import bre

# Example demonstrating character class back-references (\c)
# Standard 're' does not support directly referencing a captured group as a character class.

text = "apple banana banana orange"
# Pattern to match a word followed by a space and then the same word,
# using \c1 to reference the first captured group as a character class.
pattern_with_c = r'(\b\w+\b)\s\c1'

# Using backrefs.bre (which extends the standard 're' module)
match = bre.search(pattern_with_c, text)

if match:
    print(f"Matched: '{match.group(0)}'")
    print(f"First word captured: '{match.group(1)}'")
else:
    print("No match found.")

# Another example: replacing duplicate consecutive words
text_dupe = "hello hello world world test"
pattern_dupe = r'(\b\w+\b)\s\c1'
# Replace "word word" with just "word"
result = bre.sub(pattern_dupe, r'\1', text_dupe)
print(f"Original: '{text_dupe}'")
print(f"After replacing duplicates: '{result}'")

view raw JSON →