retrie
Efficient Trie-based regex unions for blacklist/whitelist filtering and one-pass mapping-based string replacing
What it is and what it does
Retrie solves the performance problem of matching or replacing large sets of strings using naive regex unions. Instead of compiling a pattern like `(?:abc|abs|foo)` which becomes slow as the word list grows, it builds a Trie data structure that produces a more efficient pattern like `(?:ab[cs]|foo)`. The package provides three main classes—Trie (the underlying structure), Blacklist (filter out unwanted strings), Whitelist (keep only allowed strings), and Replacer (perform bulk find-and-replace)—each with options to match whole words or substrings.
The implementation is pure Python with minimal dependencies (only typing and cached-property), making it portable and easy to integrate. It supports both Python 2.7 and modern Python versions, and has been actively maintained since its 2020 release. The API is straightforward: instantiate a class with a word list or mapping, optionally configure matching behavior, and call methods like `filter()`, `cleanse_text()`, or `replace()` on your input.
Use it for:
- Filter spam or profanity from user-generated text by maintaining a blacklist of forbidden terms.
- Extract only whitelisted keywords or entities from documents for data cleaning pipelines.
- Perform bulk find-and-replace operations (e.g., synonym substitution, URL rewriting) in a single pass.
- Build content moderation systems that need to match many patterns efficiently without regex compilation overhead.
- Normalize or standardize text by replacing multiple variant spellings or abbreviations with canonical forms.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Builds efficient Trie-based regex patterns for fast matching and replacing strings in bulk, avoiding the performance penalty of simple regex unions on large word sets.
Yes. Retrie is a focused, well-maintained tool that solves a real performance problem for bulk string matching and replacement. Its low install friction, permissive MIT license, and broad Python version support make it a safe dependency. Use it when you need to filter or replace large word sets and regex performance matters; skip it if you only have a handful of patterns to match.
Install
retrie on PyPI
pip
pip install retrieuv
uv add retriepoetry
poetry add retrieInstalling retrie
Before you install
Low friction: pure Python, no compiled dependencies, and actively maintained with a recent commit on 2026-08-01. Supports Python 2.7 through 3.12, though reliance on the unmaintained cached-property backport for older Python versions may warrant attention in long-term projects.
License in practice
MIT license permits commercial and private use with minimal restrictions—include a copy of the license and you are free to modify and distribute.
Quickstart
pip install retrie
from retrie.retrie import Blacklist
blacklist = Blacklist(["abc", "foo"], match_substrings=False)
blacklist.cleanse_text("good abc foobar")
Verify before relying
- Whether the Trie structure provides measurable performance gains for specific word-set sizes or text lengths.
- How the package handles Unicode edge cases or non-ASCII character matching in practice.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — typing, cached-property |
| Maintenance | actively maintained — 904 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 101,490/month — #12,931 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: retrie-0.3.1-py2.py3-none-any.whl
Keywords: pure-Python, regex, trie, regex-trie, blacklist, whitelist, re, search, replace
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
multiregexMatches multiple regex patterns against a…
permissive · top 15,000 on PyPI
rebulkReBulk provides a fluent API for building…
permissive · top 15,000 on PyPI
flashtextExtracts or replaces keywords in text using the…
permissive · top 5,000 on PyPI
marisa-trieProvides memory-efficient trie data structures…
copyleft · top 5,000 on PyPI
greeneryParses and manipulates regular expressions by…
permissive · top 15,000 on PyPI
textsearchFind and replace multiple strings in text with…
permissive · top 15,000 on PyPI
PyTriePyTrie provides a pure Python 3 trie data…
permissive · top 15,000 on PyPI
pygtriepygtrie provides pure Python implementations of…
permissive · top 5,000 on PyPI
repathConverts Express-style path strings (like…
permissive · top 15,000 on PyPI