Skip to main content

Use Rust regex crate in Python

Project description

Regex crate in Python

Use the fastest Regex engine ever made in Python

pip install rustregex

Important 1: Rust (cargo), Cython and a C++ compiler must be installed!

Important 2: Don't use this function in threads since the lib shares a global C++ vector for the results!

from rustcrateregex import rust_regex
from time import perf_counter
import re
import regex as cregex  # Fast Regex engine with many nice features: https://pypi.org/project/regex/

# https://www.kaggle.com/datasets/vishnu0399/server-logs?resource=download&select=logfiles.log
with open(r"C:\Users\hansc\Downloads\archive\logfiles.log", mode="rb") as f:
    testbytes = f.read()

# regex for bytes - currently bytes only
regex = rb"\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){2}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\b"

start = perf_counter()
result_rust = rust_regex((regex), (testbytes))
print(perf_counter() - start)

start = perf_counter()
result_py = [(x.start(), x.end()) for x in re.finditer((regex), testbytes)]
print(perf_counter() - start)


start = perf_counter()
result_cregex = [(x.start(), x.end()) for x in cregex.finditer((regex), testbytes)]
print(perf_counter() - start)

print("All the same: ", result_rust == result_py == result_cregex)


# Output:
# 0.6047223000005033 # Rust Regex
# 4.6312387999996645 # Python Regex
# 3.5070090999997774 # Regex
# All the same:  True

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rustcrateregex-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rustcrateregex-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file rustcrateregex-1.0.0.tar.gz.

File metadata

  • Download URL: rustcrateregex-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.9

File hashes

Hashes for rustcrateregex-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8b692614e8fcc6fea4317803f754f2a0d03cbaf3070423962cbf97de9a809793
MD5 1543d50f462d23ea5acfb81d908fb7a4
BLAKE2b-256 ea3948f5e57b921dd5574283ded7c371f2bda3fd3b5ff69f0a757114da7e1c4d

See more details on using hashes here.

File details

Details for the file rustcrateregex-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: rustcrateregex-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.9

File hashes

Hashes for rustcrateregex-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f58dd0f115653d3a82726cc0835296f40f40621d349ea4fba680b6bc73d8bfce
MD5 285b9ab1b905116aa9df4f256dd0123f
BLAKE2b-256 538c13d40d3a6ff1c1678725e5e144af8650687fdf331ce953c8c408c049a94c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page