std::match_results::str

string_type str( size_type n = 0 ) const;
(C++11 起)

返回表示指示的子匹配的字符串。

n == 0 ,则返回表示整个匹配的表达式的字符串。

n > 0 && n < size() ,则返回表示第 n 个子匹配的字符串。

n >= size() ,则返回表示不匹配的匹配的字符串。

调用等价于 string_type((*this)[n])

目录

参数

n - 指定要返回哪个匹配的整数

返回值

返回表示指定匹配或子匹配的字符串。

示例

#include <iostream>
#include <regex>
#include <string>
 
int main()
{
    std::string target("baaaby");
    std::smatch sm;
 
    std::regex re1("a(a)*b");
    std::regex_search(target, sm, re1);
    std::cout << "entire match: " << sm.str(0) << '\n'
              << "submatch #1: " << sm.str(1) << '\n';
 
    std::regex re2("a(a*)b");
    std::regex_search(target, sm, re2);
    std::cout << "entire match: " << sm.str(0) << '\n'
              << "submatch #1: " << sm.str(1) << '\n';
 
}

输出:

entire match: aaab
submatch #1: a
entire match: aaab
submatch #1: aa

参阅

返回指定的子匹配
(公开成员函数)

版本历史

  • (当前 | 先前 2017年9月29日 (五) 01:06Fruderica讨论 | 贡献. . (1,383字节) (-501). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:06P12bot讨论 | 贡献 . . (1,884字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:13P12bot讨论 | 贡献 . . (1,884字节) (-51). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 12:04P12bot讨论 | 贡献 . . (1,935字节) (+281). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 09:07P12讨论 | 贡献 . . (1,654字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,654字节) (-49). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 15:33P12讨论 | 贡献 . . (1,703字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,703字节) (+1,703). . (Translated from the English version using Google Translate)