triegex is a library that builds a compact trie-structured regular expressions from a list of words.
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
>>> 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']
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