public member function
<regex>

std::match_results::length

difference_type length (size_type n = 0) const;
Return length of match
Returns the length (in characters) of the n-th match in a match_results object that is ready.

It returns the same as the length member of its n-th sub_match element.

The match_results object shall be ready, which happens after it has been passed as the proper argument in a call to either regex_match or regex_search.

Parameters

n
Match number. This shall be lower than match_results::size.
The match number 0 represents the entire matched expression. Subsequent match numbers identify the sub-expressions, if any.
Member type size_type is an unsigned integral type.

Return value

The number of characters in sub-match n.

difference_type is a member type, defined as an alias of the difference type used by the iterator type BidirectionalIterator (the template type). This is, generally, a signed integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// match_results::length
// * using smatch, a standard alias of match_results<string::iterator>
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("subject");
  std::smatch m;
  std::regex e ("(sub)(.*)");

  std::regex_match ( s, m, e );

  for (unsigned i=0; i<m.size(); ++i) {
    std::cout << "match " << i << " (" << m[i] << ")";
    std::cout << " has a length of " << m.length(i) << std::endl;
  }

  return 0;
}


Output:
match 0 (subject) has a length of 7
match 1 (sub) has a length of 3
match 2 (ject) has a length of 4

See also