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]



另见