| Crates.io | py-regex |
| lib.rs | py-regex |
| version | 0.1.1 |
| created_at | 2025-05-08 11:06:24.757595+00 |
| updated_at | 2025-05-13 18:06:18.129041+00 |
| description | A small wrapper around the Python regex module via PyO3. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1665167 |
| size | 18,167 |
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.
regex patterns (including fuzzy matching) from Rustsearch, finditer, sub, and extract match groups, start/end positionspyo3::prepare_freethreaded_python())regex package installedpyo3 0.24.0Add this to your Cargo.toml:
[dependencies]
pyo3 = "0.24.0"
py-regex = "0.1"
Ensure that you have the Python regex module:
pip install regex
Call pyo3::prepare_freethreaded_python() once at program start:
fn main() -> pyo3::PyResult<()> {
// Initialize the Python interpreter for multi-threaded use
pyo3::prepare_freethreaded_python();
// Your code here...
Ok(())
}
use py_regex::PyRegex;
let re = PyRegex::new(r"(?i)\bhello\b")?;
assert!(re.is_match("Hello, world!")?);
// Automatically uses fuzzy thresholds per pattern
let fuzzy = PyRegex::new(r"(rust){e<=2}")?;
let m = fuzzy.find_iter("ruxy").pop();
assert!(m.is_some());
let re = PyRegex::new(r"\d+")?;
for m in re.find_iter("123 abc 456")? {
println!("Match: {:?}", m.group(0)?);
}
let re = PyRegex::new(r"\d+")?;
let result = re.replace("There are 123 apples", "NUM")?;
assert_eq!(result, "There are NUM apples");
let re = PyRegex::new(r"(\w+)@(\w+\.\w+)")?;
if let Some(m) = re.find_iter("user@example.com").pop() {
let parts = m.groups()?; // Vec<Option<String>>
// parts[0] is the full match, parts[1] username, parts[2] domain
}
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.
PyRegexMatchgroup(idx: usize) -> PyResult<Option<String>>groups() -> PyResult<Vec<Option<String>>>start(idx: usize) -> PyResult<isize>end(idx: usize) -> PyResult<isize>This project is licensed under the MIT License. See LICENSE for details.