public member function
<regex>

std::sub_match::length

difference_type length() const;
Return length
Returns the length (in characters) of the sub-match.

This is the distance between the iterators the object holds as data (members first and second) if the object status is matched. Otherwise, it is 0.

Parameters

none

Return value

The number of characters in the sub-match.

difference_type is a member type, defined as an alias of the difference type used by the iterator type BidirectionalIterator (the template parameter). 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
// sub_match::length
#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[i].length() << 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