pub struct Captures<'s> { /* private fields */ }
Expand description
Captures represents a group of captured byte strings for a single match.
The 0th capture always corresponds to the entire match. Each subsequent
index corresponds to the next capture group in the regex. If a capture
group is named, then the matched byte string is also available via the
name
method. (Note that the 0th capture is always unnamed and so must be
accessed with the get
method.)
Positions returned from a capture group are always byte indices.
's
is the lifetime of the matched subject string.
Implementations§
source§impl<'s> Captures<'s>
impl<'s> Captures<'s>
sourcepub fn get(&self, i: usize) -> Option<Match<'s>>
pub fn get(&self, i: usize) -> Option<Match<'s>>
Returns the match associated with the capture group at index i
. If
i
does not correspond to a capture group, or if the capture group
did not participate in the match, then None
is returned.
§Examples
Get the text of the match with a default of an empty string if this group didn’t participate in the match:
use pcre2::bytes::Regex;
let re = Regex::new(r"[a-z]+(?:([0-9]+)|([A-Z]+))")?;
let caps = re.captures(b"abc123")?.unwrap();
let text1 = caps.get(1).map_or(&b""[..], |m| m.as_bytes());
let text2 = caps.get(2).map_or(&b""[..], |m| m.as_bytes());
assert_eq!(text1, &b"123"[..]);
assert_eq!(text2, &b""[..]);
Trait Implementations§
source§impl<'s, 'i> Index<&'i str> for Captures<'s>
impl<'s, 'i> Index<&'i str> for Captures<'s>
Get a group by name.
's
is the lifetime of the matched subject string and 'i
is the lifetime
of the group name (the index).
The text can’t outlive the Captures
object if this method is
used, because of how Index
is defined (normally a[i]
is part
of a
and can’t outlive it); to do that, use name
instead.
§Panics
If there is no group named by the given value.
source§impl<'s> Index<usize> for Captures<'s>
impl<'s> Index<usize> for Captures<'s>
Get a group by index.
's
is the lifetime of the matched subject string.
The subject can’t outlive the Captures
object if this method is
used, because of how Index
is defined (normally a[i]
is part
of a
and can’t outlive it); to do that, use get()
instead.
§Panics
If there is no group at the given index.