Python中的Trie-化正则表达式
项目描述
*******
triegex
*******
.. image:: https://travis-ci.org/ZhukovAlexander/triegex.svg?branch=master
:target: https://travis-ci.org/ZhukovAlexander/triegex
关于
######
**triegex**是一个库,可以从单词列表构建紧凑的trie结构正则表达式。
安装
########
.. code-block:: bash
pip install triegex
示例用法
########
.. code-block:: python
>>> import triegex
>>>
>>> t = triegex.Triegex('foo', 'bar', 'baz')
>>>
>>> t.to_regex() # 构建正则表达式
'(?:ba(?:r\\b|z\\b)|foo\\b|~^(?#match nothing))'
>>>
>>> t.add('spam')
>>>
>>> 'spam' in t # 检查单词是否在其中
True
>>>
>>> import re
>>> re.findall(t.to_regex(), 'spam & eggs') # ['spam']
['spam']
为什么?
####
该库受需要匹配IANA顶级域名列表(`这是一个相当大的列表 <http://data.iana.org/TLD/tlds-alpha-by-domain.txt>`_)的需求所启发。
也有趣
**triegex**受以下项目的影响:`frak <https://github.com/noprompt/frak>`_, `regex-trie <https://github.com/alexeld/regex-trie>`_ 和 `Regexp-Trie <http://search.cpan.org/~dankogai/Regexp/Trie-0.02/lib/Regexp/Trie.pm>`_
triegex
*******
.. image:: https://travis-ci.org/ZhukovAlexander/triegex.svg?branch=master
:target: https://travis-ci.org/ZhukovAlexander/triegex
关于
######
**triegex**是一个库,可以从单词列表构建紧凑的trie结构正则表达式。
安装
########
.. code-block:: bash
pip install triegex
示例用法
########
.. code-block:: python
>>> import triegex
>>>
>>> t = triegex.Triegex('foo', 'bar', 'baz')
>>>
>>> t.to_regex() # 构建正则表达式
'(?:ba(?:r\\b|z\\b)|foo\\b|~^(?#match nothing))'
>>>
>>> t.add('spam')
>>>
>>> 'spam' in t # 检查单词是否在其中
True
>>>
>>> import re
>>> re.findall(t.to_regex(), 'spam & eggs') # ['spam']
['spam']
为什么?
####
该库受需要匹配IANA顶级域名列表(`这是一个相当大的列表 <http://data.iana.org/TLD/tlds-alpha-by-domain.txt>`_)的需求所启发。
也有趣
**triegex**受以下项目的影响:`frak <https://github.com/noprompt/frak>`_, `regex-trie <https://github.com/alexeld/regex-trie>`_ 和 `Regexp-Trie <http://search.cpan.org/~dankogai/Regexp/Trie-0.02/lib/Regexp/Trie.pm>`_