public member function
<regex>

std::match_results::prefix

const_reference prefix() const;
返回字首
返回對 sub_match 物件的引用,該物件表示目標序列中與匹配序列之前的字元序列。

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

引數



返回值

對描述字首的 sub_match 物件的引用。

成員型別const_reference是引用適當的 const sub_match 型別的別名。

示例

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

int main ()
{
  std::string s ("there is a needle in this haystack");
  std::smatch m;
  std::regex e ("needle");

  std::cout << "searching for needle in [" << s << "]\n";
  std::regex_search ( s, m, e );

  if (m.ready()) {
    std::cout << m[0] << " found!\n";
    std::cout << "prefix: [" << m.prefix() << "]\n";
    std::cout << "suffix: [" << m.suffix() << "]\n";
  }

  return 0;
}

輸出
searching for needle in [there is a needle in this haystack]
needle found!
prefix: [there is a ]
suffix: [ in this haystack]



另見