pub struct MatchInfo {
pub matched_text: String,
pub start: usize,
pub end: usize,
pub groups: Vec<Option<String>>,
pub group_positions: Vec<Option<(usize, usize)>>,
pub named_groups: BTreeMap<String, String>,
}Expand description
Information about a single regex match, including captured groups.
Positions (start, end, group_positions) are char indices, not byte indices.
Fields§
§matched_text: StringThe full matched text.
start: usizeStart position (char index) of the match.
end: usizeEnd position (char index, exclusive) of the match.
groups: Vec<Option<String>>Captured group texts, indexed from 0 (group 1 is groups[0]).
group_positions: Vec<Option<(usize, usize)>>Captured group positions as (start, end) char indices.
named_groups: BTreeMap<String, String>Named group captures, keyed by group name.
Implementations§
Source§impl MatchInfo
impl MatchInfo
Sourcepub fn group(&self, n: usize) -> Option<&str>
pub fn group(&self, n: usize) -> Option<&str>
Look up a numbered capture group, mirroring java.util.regex.Matcher.group(int).
n == 0 returns the full match. n >= 1 returns the n-th capture
group, or None if that group did not participate in the match.
let re = Regex::new(r"(\w+)@(\w+)").unwrap();
let m = re.find_iter("alice@example.com").next().unwrap();
assert_eq!(m.group(0), Some("alice@example"));
assert_eq!(m.group(1), Some("alice"));
assert_eq!(m.group(2), Some("example"));
assert_eq!(m.group(99), None);Sourcepub fn name(&self, name: &str) -> Option<&str>
pub fn name(&self, name: &str) -> Option<&str>
Look up a named capture group, mirroring java.util.regex.Matcher.group(String).
Returns None if the group does not exist or did not participate in
the match.
let re = Regex::new(r"(?<user>\w+)@(?<host>\w+\.\w+)").unwrap();
let m = re.find_iter("alice@example.com").next().unwrap();
assert_eq!(m.name("user"), Some("alice"));
assert_eq!(m.name("host"), Some("example.com"));
assert_eq!(m.name("nope"), None);Sourcepub fn group_count(&self) -> usize
pub fn group_count(&self) -> usize
Number of capture groups in the pattern (not counting group 0).
Mirrors java.util.regex.Matcher.groupCount.