Struct PyRegexMatch

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

A wrapper for the match object from the Python regex module.

Implementations§

Source§

impl PyRegexMatch

Source

pub fn group(&self, group: u16) -> PyResult<Option<String>>

Returns the match for the specified group. For example, group(0) is the entire match, group(1) is the first subgroup, etc.

Examples found in repository?
examples/basic.rs (line 15)
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 groups(&self) -> PyResult<Vec<Option<String>>>

Returns all captured groups as a vector. Analogous to Python’s groups() method, which returns a tuple of all subgroups (starting from 1).

Source

pub fn groupdict(&self) -> PyResult<HashMap<String, Option<String>>>

Returns the named groups dictionary (groupdict()) as a HashMap.

Examples found in repository?
examples/basic.rs (line 17)
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 start(&self, group: u16) -> PyResult<isize>

Returns the start position of the match for the specified group.

Examples found in repository?
examples/basic.rs (line 18)
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 end(&self, group: u16) -> PyResult<isize>

Returns the end position of the match for the specified group.

Examples found in repository?
examples/basic.rs (line 18)
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}

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,