Skip to main content

MatchInfo

Struct MatchInfo 

Source
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: String

The full matched text.

§start: usize

Start position (char index) of the match.

§end: usize

End 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

Source

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);
Source

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);
Source

pub fn group_count(&self) -> usize

Number of capture groups in the pattern (not counting group 0). Mirrors java.util.regex.Matcher.groupCount.

Trait Implementations§

Source§

impl Clone for MatchInfo

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MatchInfo

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for MatchInfo

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for MatchInfo

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.