public member function
<regex>

std::match_results::str

string_type str (size_type n = 0) const;
將匹配項作為字串返回
返回一個string,其中包含match_results物件中第n個匹配項的內容,該物件是ready的。

以下返回的物件str是屬於basic_stringcorresponding 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 incmatch.

match_results物件應為ready,這在將其作為正確引數傳遞給regex_matchregex_search的呼叫時發生。

此函式返回的內容與nsub_match元素的str成員相同。

引數

n
匹配編號。 此編號應低於match_results::size
匹配編號0表示整個匹配的表示式。 如果有任何子表示式,則後續的匹配編號標識這些子表示式。
成員型別size_type是一種無符號整型型別。

返回值

具有第n個匹配項的string物件。

string_type是一種成員型別,定義為basic_string對應於正在迭代的字元的型別,別名是BidirectionalIterator(模板型別)。 也就是說,對於迭代所有物件的stringchars(如cmatchsmatch).

示例

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;
}

輸出
matches:
subject
sub
ject


另見