py_regex
A small Rust library providing a convenient wrapper around the Python regex
module
via PyO3.
It allows you to compile fuzzy (approximate) regular expressions with per-pattern thresholds and use Python’s advanced
regex engine directly from Rust.
Features
- Compile Python
regex
patterns (including fuzzy matching) from Rust - Set individual error thresholds per pattern (insertion, deletion, substitution)
- Call
search
,finditer
,sub
, and extract match groups, start/end positions - Thread-safe (after calling
pyo3::prepare_freethreaded_python()
) - Minimal dependencies
Requirements
- Python 3.x with the
regex
package installed pyo3
0.24.0
Installation
Add this to your Cargo.toml
:
[]
= "0.24.0"
= "0.1"
Ensure that you have the Python regex
module:
Quick Start
Call pyo3::prepare_freethreaded_python()
once at program start:
Compile a Pattern
use PyRegex;
let re = new?;
assert!;
Fuzzy Matching
// Automatically uses fuzzy thresholds per pattern
let fuzzy = new?;
let m = fuzzy.find_iter.pop;
assert!;
Find All Matches
let re = new?;
for m in re.find_iter?
Replace (Substitution)
let re = new?;
let result = re.replace?;
assert_eq!;
Extract Groups
let re = new?;
if let Some = re.find_iter.pop
API
PyRegex::new(pattern: &str) -> PyResult<PyRegex>
Compile a Python regex
pattern.
PyRegex::is_match(text: &str) -> PyResult<bool>
Return true
if search(text)
finds a match.
PyRegex::find_iter(text: &str) -> PyResult<Vec<PyRegexMatch>>
Return all non-overlapping matches as PyRegexMatch
.
PyRegex::replace(text: &str, replacement: &str) -> PyResult<String>
Perform substitution (sub
) on the input text.
PyRegexMatch
group(idx: usize) -> PyResult<Option<String>>
groups() -> PyResult<Vec<Option<String>>>
start(idx: usize) -> PyResult<isize>
end(idx: usize) -> PyResult<isize>
License
This project is licensed under the MIT License. See LICENSE for details.