Skip to main content

Transform set of words to efficient regular expression

Project description

trrex logo
Trrex Downloads Package Status Code Coverage Status PyPI Version Documentation Status

Efficient keyword mining with regular expressions

This package includes a pure Python function that enables you to represent a set of keywords (strings) as an efficient regular expression. With this regular expression, you can perform various operations, such as replacing and extracting keywords. The name of the package comes from the internal trie used to build the regular expression (trie to regex)

Install trrex

Use pip,

pip install trrex


import trrex as tx
import re

pattern = tx.make(['baby', 'bat', 'bad'])
hits = re.findall(pattern, 'The baby was scared by the bad bat.')
# hits = ['baby', 'bat', 'bad']


import trrex as tx
import pandas as pd

frame = pd.DataFrame({
    "txt": ["The baby", "The bat"]
pattern = tx.make(['baby', 'bat', 'bad'], prefix=r"\b(", suffix=r")\b") # need to specify capturing groups
frame["match"] = frame["txt"].str.extract(pattern)
hits = frame["match"].tolist()
# hits = ['baby', 'bad']

Why use trrex?

  • trrex builds a better regex pattern, than the simple regex union, therefore searching (and replacing) keywords is about 300 times faster than a regex union pattern, and about 2.5 times faster than FlashText algorithm. See below for a performance comparison:

Performance comparison

  • Plays well with others, can be integrated easily with pandas, spacy and any other regex engine. See the documentation for examples.
  • Pure Python, no other dependencies


If you have any issues with this repository, please don't hesitate to raise them. It is actively maintained, and we will do our best to help you.


This project is based on the following resources:

Liked the work?

If you've found this repository helpful, why not give it a star? It's an easy way to show your appreciation and support for the project. Plus, it helps others discover it too!

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trrex-0.0.5.tar.gz (7.4 kB view hashes)

Uploaded source

Built Distribution

trrex-0.0.5-py3-none-any.whl (7.2 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page