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

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

Captures represents a group of captured 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 string is also available via the `name` method. (Note that the 0th capture is always unnamed and so must be accessed with the `at` method.)

Positions returned from a capture group are always byte indices.

`'t` is the lifetime of the matched text.

## Methods

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

#### `fn pos(&self, i: usize) -> Option<(usize, usize)>`

Returns the start and end positions of the Nth capture group. Returns `None` if `i` is not a valid capture group or if the capture group did not match anything. The positions returned are always byte indices with respect to the original string matched.

#### `fn at(&self, i: usize) -> Option<&'t str>`

Returns the matched string for the capture group `i`. If `i` isn't a valid capture group or didn't match anything, then `None` is returned.

#### `fn name(&self, name: &str) -> Option<&'t str>`

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

#### `fn iter(&'t self) -> SubCaptures<'t>`

Creates an iterator of all the capture groups in order of appearance in the regular expression.

#### `fn iter_pos(&'t self) -> SubCapturesPos<'t>`

Creates an iterator of all the capture group positions in order of appearance in the regular expression. Positions are byte indices in terms of the original string matched.

#### `fn iter_named(&'t self) -> SubCapturesNamed<'t>`

Creates an iterator of all named groups as an tuple with the group name and the value. The iterator returns these values in arbitrary order.

#### `fn expand(&self, text: &str) -> String`

Expands all instances of `$name` in `text` to the corresponding capture group `name`.

`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.

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

#### `fn len(&self) -> usize`

Returns the number of captured groups.

#### `fn is_empty(&self) -> bool`

Returns true if and only if there are no captured groups.

## Trait Implementations

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

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

### `impl<'t> Index<usize> for Captures<'t>`

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

# Panics

If there is no group at the given index.

#### `type Output = str`

#### `fn index(&self, i: usize) -> &str`

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

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 = str`

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

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## 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`)