skillfed

retrie

Efficient Trie-based regex unions for blacklist/whitelist filtering and one-pass mapping-based string replacing

retrie v0.3.1 101.5K downloads/30d#12,931 on PyPI76
Permissive license MIT Active released

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 retrie

uv

uv add retrie

poetry

poetry add retrie

Installing 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 2Programming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Topic :: Software Development :: Libraries :: Python ModulesTopic :: Utilities

Tags

trie regex pattern matchingefficient string replacementblacklist whitelist filteringbulk regex matchingtrie-based text searchfast regex unionsstring filtering performance
text-processingregex-optimizationdata-filtering

More Python Modules packages