/triegex

Build a trie-structured regular expression from a list of words

Primary LanguagePythonMIT LicenseMIT

triegex

https://travis-ci.org/ZhukovAlexander/triegex.svg?branch=master

About

triegex is a library that builds a compact trie-structured regular expressions from a list of words.

Installation

pip install triegex

Alternatively, you can install the latest release directly from git:

pip install git+https://github.com/ZhukovAlexander/triegex.git@0.0.4

Example usage

>>> import triegex
>>>
>>> t = triegex.Triegex('foo', 'bar', 'baz')
>>>
>>> t.to_regex()  # build regular expression
'(?:ba(?:r\\b|z\\b)|foo\\b|~^(?#match nothing))'
>>>
>>> t.add('spam')
>>>
>>> 'spam' in t  # you check if the word is in there
True
>>>
>>> import re
>>> re.findall(t.to_regex(), 'spam & eggs')  # ['spam']
['spam']

Why?

The library was inspired by a need to match a list of valid IANA top-level domain names (which is pretty big).

Also it's fun

triegex was influenced by these projects: frak, regex-trie and Regexp-Trie