跳转到主要内容

未提供项目描述

项目描述

regex-rust (regexrs)

利用Rust的regex crate和PyO3创建与Python标准库中re模块类似的接口。

pip install regex-rust
>>> import regexrs as re
>>> pattern = re.compile(r'(\w+) (\w+)')
>>> m = pattern.match('hello rust')
>>> m.groups()
('hello', 'rust')
>>> m.pos
0
>>> m.endpos
10
>>> re.findall(r'\w+', 'hello rust')
['hello', 'rust']
>>> re.fullmatch(r'\w+', 'foo')
<regexrs.Match object; span=(0, 3), match="foo">

基准测试

benchmark.py主要借鉴自regex-benchmark项目。您需要将input-text.txt文件的路径传递给benchmark.py

这个简单的基准测试表明,regexrs可能比标准库中的re模块或甚至regex库在某种情况下要快得多。请注意,这个基准测试只是在单个大型文本输入上测试了三个简单的用例。因此,我们可以从这个基准测试中得到的见解相当有限。在某些情况下,regexrs可能比regex慢2倍,尤其是在需要创建Match对象时。

在Windows AMD64 Python 3.12.2上测试的结果 - 时间单位为毫秒(越低越好)

测试 regexrs re (stdlib) regex 与re比较
Email 12.51 354.53 690.15 28.34倍更快
URI 4.82 282.69 430.26 58.65倍更快
IP 4.71 321.37 25.43 68.23倍更快

如何运行基准测试

# be sure to have run `pip install regex-rust` first
# to test regexrs:
python benchmark.py /path/to/input-text.txt

# to test stdlib re:
python benchmark.py /path/to/input-text.txt re

# be sure to have run `pip install regex` first
# to test regex library:
python benchmark.py /path/to/input-text.txt regex

从源安装的方法

您可以使用pip进行构建和安装。

pip install .

如果您想手动构建

pip install maturin
maturin build --release

状态

大多数功能不完整,可能存在很多错误。我主要用它来练习使用Rust和PyO3创建和分发Python扩展。不清楚这个项目是否会变得非常有用。如果您今天正在寻找一个完整且性能良好的Python正则表达式库,请查看PyPI上的regex项目

与标准库的差异

  • endpos参数通常在re模块中找到,但在regexrsmatch/search/findall/finditer方法中不受支持。
  • 某些正则表达式功能不受支持(因为这些功能在regexcrate中不受支持),例如前瞻和后引用。
  • 不是所有标志都受支持。在当前版本中,您可以使用IGNORECASEMULTILINEDOTALLVERBOSE(或它们的简写等效项)。这些标志被转换为内联标志,并添加到您提供的模式前面。
  • 在未来的版本之前,没有缓存来避免多次重新编译相同的模式

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面