pub struct Captures<'t> { /* 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.

't is the lifetime of the matched text.

Implementations

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:

let re = Regex::new(r"[a-z]+(?:([0-9]+)|([A-Z]+))").unwrap();
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""[..]);

Returns the match for the capture group named name. If name isn’t a valid capture group or didn’t match anything, then None is returned.

An iterator that yields all capturing matches in the order in which they appear in the regex. If a particular capture group didn’t participate in the match, then None is yielded for that capture.

The first match always corresponds to the overall match of the regex.

Expands all instances of $name in replacement to the corresponding capture group name, and writes them to the dst buffer given.

name may be an integer corresponding to the index of the capture group (counted by order of opening parenthesis where 0 is the entire match) or it can be a name (consisting of letters, digits or underscores) corresponding to a named capture group.

If name isn’t a valid capture group (whether the name doesn’t exist or isn’t a valid index), then it is replaced with the empty string.

The longest possible name consisting of the characters [_0-9A-Za-z] is used. e.g., $1a looks up the capture group named 1a and not the capture group at index 1. To exert more precise control over the name, or to refer to a capture group name that uses characters outside of [_0-9A-Za-z], use braces, e.g., ${1}a or ${foo[bar].baz}. When using braces, any sequence of valid UTF-8 bytes is permitted. If the sequence does not refer to a capture group name in the corresponding regex, then it is replaced with an empty string.

To write a literal $ use $$.

Returns the number of captured groups.

This is always at least 1, since every regex has at least one capture group that corresponds to the full match.

Trait Implementations

Formats the value using the given formatter. Read more

Get a group by name.

't is the lifetime of the matched text 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.

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Get a group by index.

't is the lifetime of the matched text.

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 get() instead.

Panics

If there is no group at the given index.

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Use this to cast from one trait object type to another. Read more

Use this to upcast a trait to one of its supertraits. Read more

Use this to cast from one trait object type to another. This method is more customizable than the dyn_cast method. Here you can also specify the “source” trait from which the cast is defined. This can for example allow using casts from a supertrait of the current trait object. Read more

Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more