Capture groups from a regex match
Contains the matched text and positions of capture groups
Methods
impl Captures { ... }
-
Get the full matched text (capture group 0)
Example
use std::regex::Regex; let re = Regex::compile("\\d+").unwrap(); defer re.free(); let caps = re.captures("foo 123 bar").unwrap(); defer caps.free(); assert_eq!(caps.get_match().unwrap(), "123");Run this example -
Get a capture group by index (0 = full match, 1+ = user groups)
Example
use std::regex::Regex; let re = Regex::compile("(\\w+)@(\\w+)").unwrap(); defer re.free(); let caps = re.captures("user@example.com").unwrap(); defer caps.free(); assert_eq!(caps.get(0).unwrap(), "user@example"); assert_eq!(caps.get(1).unwrap(), "user"); assert_eq!(caps.get(2).unwrap(), "example");Run this example -
Get the start and end positions of a capture group
Example
use std::regex::Regex; let re = Regex::compile("(\\d+)").unwrap(); defer re.free(); let caps = re.captures("abc123def").unwrap(); defer caps.free(); assert_eq!(caps.get_pos(1).unwrap(), (3usize, 6usize));Run this example -
Get the number of capture groups (including group 0 for full match)
Example
use std::regex::Regex; let re = Regex::compile("(\\w+)@(\\w+)").unwrap(); defer re.free(); let caps = re.captures("user@example.com").unwrap(); defer caps.free(); assert_eq!(caps.len(), 3); // Group 0 (full match) + 2 user groupsRun this example -
fn free(self: &mut Captures)
Frees the memory backing the object.