pub struct PyRegexMatch { /* private fields */ }
Expand description
A wrapper for the match object from the Python regex
module.
Implementations§
Source§impl PyRegexMatch
impl PyRegexMatch
Sourcepub fn group(&self, group: u16) -> PyResult<Option<String>>
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}
Sourcepub fn groups(&self) -> PyResult<Vec<Option<String>>>
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).
Sourcepub fn groupdict(&self) -> PyResult<HashMap<String, Option<String>>>
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}
Sourcepub fn start(&self, group: u16) -> PyResult<isize>
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}
Sourcepub fn end(&self, group: u16) -> PyResult<isize>
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§
impl Freeze for PyRegexMatch
impl !RefUnwindSafe for PyRegexMatch
impl Send for PyRegexMatch
impl Sync for PyRegexMatch
impl Unpin for PyRegexMatch
impl UnwindSafe for PyRegexMatch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more