Skip to content

Latest commit

 

History

History
54 lines (38 loc) · 1.37 KB

README.rst

File metadata and controls

54 lines (38 loc) · 1.37 KB

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/[email protected]

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