# [−] [src] Struct regex::bytes::Captures

    pub struct Captures<'t> { /* fields omitted */ }

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.

## Methods

### `impl<'t> Captures<'t>` [src]

#### `pub fn get(&self, i: usize) -> Option<Match<'t>>` [src]

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""[..]);

#### `pub fn name(&self, name: &str) -> Option<Match<'t>>` [src]

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.

####
ⓘImportant traits for SubCaptureMatches<'c, 't>

### Important traits for SubCaptureMatches<'c, 't>

`impl<'c, 't> Iterator for SubCaptureMatches<'c, 't> type Item = Option<Match<'t>>;`
`pub fn iter<'c>(&'c self) -> SubCaptureMatches<'c, 't>` [src]

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.

#### `pub fn expand(&self, replacement: &[u8], dst: &mut Vec<u8>)` [src]

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 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, use braces, e.g., `${1}a`.

To write a literal `$` use `$$`.

#### `pub fn len(&self) -> usize` [src]

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

### `impl<'t> Debug for Captures<'t>` [src]

#### `fn fmt(&self, f: &mut Formatter) -> Result` [src]

Formats the value using the given formatter. Read more

### `impl<'t> Index<usize> for Captures<'t>` [src]

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.

#### `type Output = [u8]`

The returned type after indexing.

#### `fn index(&self, i: usize) -> &[u8]` [src]

Performs the indexing (`container[index]`) operation.

### `impl<'t, 'i> Index<&'i str> for Captures<'t>` [src]

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.

#### `type Output = [u8]`

The returned type after indexing.

#### `fn index<'a>(&'a self, name: &'i str) -> &'a [u8]` [src]

Performs the indexing (`container[index]`) operation.

## Auto Trait Implementations

### `impl<'t> Send for Captures<'t>`

### `impl<'t> Sync for Captures<'t>`

## Blanket Implementations

### `impl<T> From<T> for T` [src]

#### `fn from(t: T) -> T` [src]

Performs the conversion.

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U` [src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)