Struct PyRegex

Source
pub struct PyRegex { /* private fields */ }
Expand description

A wrapper for a compiled regular expression from the Python regex library.

Implementations§

Source§

impl PyRegex

Source

pub fn new(pattern: &str) -> PyResult<Self>

Creates a new regular expression by compiling the pattern via Python’s regex.compile.

Examples found in repository?
examples/basic.rs (line 11)
5fn main() -> PyResult<()> {
6    // Initialize Python for multithreaded usage.
7    pyo3::prepare_freethreaded_python();
8
9    let pattern = r"(?P<id>\d+)";
10    let text = "IDs: 101, 202, 303";
11    let re = Arc::new(PyRegex::new(pattern)?);
12
13    // Example usage of `search_match()` to obtain a `PyRegexMatch` object.
14    if let Some(m) = re.search_match(text)? {
15        println!("Full match (group 0): {:?}", m.group(0)?);
16        println!("Group 'id' (as group 0 here): {:?}", m.group(0)?);
17        println!("Groupdict: {:?}", m.groupdict()?);
18        println!("Span for group 0: {}..{}", m.start(0)?, m.end(0)?);
19    }
20
21    // Example of multithreaded usage of `find_iter()`, returning a `Vec<PyRegexMatch>`.
22    let mut handles = vec![];
23    for i in 0..4 {
24        let re_clone = Arc::clone(&re);
25        let text_clone = text.to_string();
26        let handle = thread::spawn(move || -> PyResult<()> {
27            let matches = re_clone.find_iter(&text_clone)?;
28            println!("Thread {}: found {} matches.", i, matches.len());
29            for m in matches {
30                println!("Thread {}: match group 0: {:?}", i, m.group(0)?);
31            }
32            Ok(())
33        });
34        handles.push(handle);
35    }
36
37    for handle in handles {
38        handle.join().unwrap()?;
39    }
40
41    Ok(())
42}
Source

pub fn search_match(&self, text: &str) -> PyResult<Option<PyRegexMatch>>

Performs a search for the first match and returns a PyRegexMatch object.

Examples found in repository?
examples/basic.rs (line 14)
5fn main() -> PyResult<()> {
6    // Initialize Python for multithreaded usage.
7    pyo3::prepare_freethreaded_python();
8
9    let pattern = r"(?P<id>\d+)";
10    let text = "IDs: 101, 202, 303";
11    let re = Arc::new(PyRegex::new(pattern)?);
12
13    // Example usage of `search_match()` to obtain a `PyRegexMatch` object.
14    if let Some(m) = re.search_match(text)? {
15        println!("Full match (group 0): {:?}", m.group(0)?);
16        println!("Group 'id' (as group 0 here): {:?}", m.group(0)?);
17        println!("Groupdict: {:?}", m.groupdict()?);
18        println!("Span for group 0: {}..{}", m.start(0)?, m.end(0)?);
19    }
20
21    // Example of multithreaded usage of `find_iter()`, returning a `Vec<PyRegexMatch>`.
22    let mut handles = vec![];
23    for i in 0..4 {
24        let re_clone = Arc::clone(&re);
25        let text_clone = text.to_string();
26        let handle = thread::spawn(move || -> PyResult<()> {
27            let matches = re_clone.find_iter(&text_clone)?;
28            println!("Thread {}: found {} matches.", i, matches.len());
29            for m in matches {
30                println!("Thread {}: match group 0: {:?}", i, m.group(0)?);
31            }
32            Ok(())
33        });
34        handles.push(handle);
35    }
36
37    for handle in handles {
38        handle.join().unwrap()?;
39    }
40
41    Ok(())
42}
Source

pub fn find_iter(&self, text: &str) -> PyResult<Vec<PyRegexMatch>>

Returns a list of PyRegexMatch objects from finditer().

Examples found in repository?
examples/basic.rs (line 27)
5fn main() -> PyResult<()> {
6    // Initialize Python for multithreaded usage.
7    pyo3::prepare_freethreaded_python();
8
9    let pattern = r"(?P<id>\d+)";
10    let text = "IDs: 101, 202, 303";
11    let re = Arc::new(PyRegex::new(pattern)?);
12
13    // Example usage of `search_match()` to obtain a `PyRegexMatch` object.
14    if let Some(m) = re.search_match(text)? {
15        println!("Full match (group 0): {:?}", m.group(0)?);
16        println!("Group 'id' (as group 0 here): {:?}", m.group(0)?);
17        println!("Groupdict: {:?}", m.groupdict()?);
18        println!("Span for group 0: {}..{}", m.start(0)?, m.end(0)?);
19    }
20
21    // Example of multithreaded usage of `find_iter()`, returning a `Vec<PyRegexMatch>`.
22    let mut handles = vec![];
23    for i in 0..4 {
24        let re_clone = Arc::clone(&re);
25        let text_clone = text.to_string();
26        let handle = thread::spawn(move || -> PyResult<()> {
27            let matches = re_clone.find_iter(&text_clone)?;
28            println!("Thread {}: found {} matches.", i, matches.len());
29            for m in matches {
30                println!("Thread {}: match group 0: {:?}", i, m.group(0)?);
31            }
32            Ok(())
33        });
34        handles.push(handle);
35    }
36
37    for handle in handles {
38        handle.join().unwrap()?;
39    }
40
41    Ok(())
42}
Source

pub fn is_match(&self, text: &str) -> PyResult<bool>

Source

pub fn find_all(&self, text: &str) -> PyResult<Vec<String>>

Source

pub fn replace(&self, text: &str, replacement: &str) -> PyResult<String>

Source

pub fn split(&self, text: &str) -> PyResult<Vec<String>>

Source

pub fn escape( str: &str, special_only: bool, literal_spaces: bool, ) -> PyResult<String>

Escapes a string.

Trait Implementations§

Source§

impl Debug for PyRegex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,