Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

regex-rust

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

regex-rust

  • 0.4.0
  • PyPI
  • Socket score

Maintainers
1

regex-rust (regexrs)

Leverages the Rust regex crate with PyO3 to create an interface similar to the Python standard library re module.

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">

Benchmarks

benchmark.py is largely borrowed from the regex-benchmark project. You are expected to pass in a path to the file of the input-text.txt file to benchmark.py.

This simple benchmark suggests that regexrs may be significantly faster than the re module from the standard library or even the regex library, at least in some use cases. Keep in mind that this benchmark tests just three simple use cases on a single large text input. Therefore, the insights we can infer from this benchmark are quite limited. In some cases, regexrs may be up to 2x slower than regex, especially when creation of Match objects is necessary.

Results as tested on Windows AMD64 Python 3.12.2 - times in ms (lower is better):

testregexrsre (stdlib)regexCompared to re
Email12.51354.53690.1528.34x faster
URI4.82282.69430.2658.65x faster
IP4.71321.3725.4368.23x faster

To run the benchmarks yourself:

# 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

How to install from source

You can use pip to build and install.

pip install .

If you want to build manually:

pip install maturin
maturin build --release

Status

Mostly incomplete and likely very buggy. I am using this mostly as an exercise in creating and distributing Python extensions using Rust and PyO3. It's unclear if this will ever be a particularly useful project or not. If you're looking for a complete and performant regex library for Python today, see the regex project on PyPI.

Differences compared to standard lib:

  • The endpos argument normally found in the re module is not supported in regexrs for the match/search/findall/finditer methods.
  • Some regex features are not supported (because they are not supported by the regex crate), such as lookarounds and backreferences.
  • Not all flags are supported. At present release, you may use the flags IGNORECASE, MULTILINE, DOTALL and VERBOSE (or their shorthand equivalents). These are translated to inline flags and prepended to your given pattern.
  • Until a future release, there is no cache for avoiding re-compiling the same patterns multiple times

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc