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.3.tar.gz (7.7 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.3-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rustcrateregex-1.0.3.tar.gz
  • Upload date:
  • Size: 7.7 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.3.tar.gz
Algorithm Hash digest
SHA256 3afe48ece2f105636d8f9889b18dd701a36096165aaa1617d5251b8fc7f4a4a4
MD5 51e04a525dbcf8fee7bede26efd8cb9f
BLAKE2b-256 c8799f5c6aba3ab02c7689532106139a9f4d48fb55f29b2e13716060fd206809

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rustcrateregex-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.1 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 562faf51a1bb5c8e5742208593329744aad2ec216ce4403712b003c05df9b4b4
MD5 af70cff0382be3b2f64830a5f478e671
BLAKE2b-256 edfd505b8d54bed3408c4625cac894c1a95d86e90f7fbe5b168cd274b0034ff2

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