public member function
<regex>

std::match_results::str

string_type str (size_type n = 0) const;
Return match as a string
Returns a string with the contents of the n-th match in a match_results object that is ready.

The object returned by str is of the basic_string corresponding to the type of the characters in the target sequence, even if the match_results object is filled using other types of character sequences, like the C-strings used in cmatch.

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.

This function returns the same as the str member of its n-th sub_match element.

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

A string object with the n-th match.

string_type is a member type, defined as an alias of the basic_string type corresponding to the characters being iterated by BidirectionalIterator (the template type). I.e., string for all objects that iterate on chars (like cmatch and smatch).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// match_results::str
// - using cmatch, a standard alias of match_results<const char*>
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  using namespace std::regex_constants;

  std::cmatch m;

  std::regex_match ( "subject", m, std::regex("(sub)(.*)") );

  std::string output = "matches:\n";
  for (unsigned i=0; i<m.size(); ++i) {
    output+= m.str(i) + "\n";
  }

  std::cout << output << std::endl;

  return 0;
}


Output:
matches:
subject
sub
ject

See also