<regex>

public member function
<regex>

std::match_results::position

difference_type position (size_type n = 0) const;
Return position of match
Returns the position within the target sequence of the first character of the n-th match in the match_results object that is ready.

This is the distance between the first character in the target sequence and the first character matched.

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 position of the match in the sequence.

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::position
// * using smatch, a standard alias of match_results<string::iterator>
#include <iostream>
#include <string>
#include <regex>

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

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

  for (unsigned i=0; i<m.size(); ++i) {
    std::cout << "match " << i << " (" << m[i] << ") ";
    std::cout << "at position " << m.position(i) << std::endl;
  }

  return 0;
}


Output:
match 0 (subject) at position 5
match 1 (sub) at position 5
match 2 (ject) at position 8

See also