std::smatch 返回什么,你应该如何使用它? [英] What is returned in std::smatch and how are you supposed to use it?

查看:497
本文介绍了std::smatch 返回什么,你应该如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string 我 5 岁了"

正则表达式 "(?!am )\d"

如果您转到 http://regexr.com/ 并将正则表达式应用于字符串,您将得到 5.我想用 std::regex 得到这个结果,但我不明白如何使用匹配结果,可能还必须更改正则表达式.

if you go to http://regexr.com/ and apply regex to the string you'll get 5. I would like to get this result with std::regex, but I do not understand how to use match results and probably regex has to be changed as well.

std::regex expression("(?!am )\\d");
std::smatch match;
std::string what("I am 5 years old.");
if (regex_search(what, match, expression))
{
     //???
}

推荐答案

std::smatchmatch_results 用于匹配字符串对象的类模板(使用 string::const_iterator 作为其迭代器类型).此类的成员是为 match_results 描述的成员,但使用 string::const_iterator 作为其 BidirectionalIterator 模板参数.

The std::smatch is an instantiation of the match_results class template for matches on string objects (with string::const_iterator as its iterator type). The members of this class are those described for match_results, but using string::const_iterator as its BidirectionalIterator template parameter.

std::match_results 支持一个 operator[]:

如果 n >0n ,返回对 std::sub_match<的引用/code> 表示与第 n 个捕获的 标记子表达式).

If n > 0 and n < size(), returns a reference to the std::sub_match representing the part of the target sequence that was matched by the nth captured marked subexpression).

如果 n == 0,则返回对 的引用std::sub_match 表示整个匹配正则表达式匹配的目标序列部分.

If n == 0, returns a reference to the std::sub_match representing the part of the target sequence matched by the entire matched regular expression.

如果 n >= size(),则返回对 std::sub_match 表示不匹配的子表达式(目标序列的空子范围).

if n >= size(), returns a reference to a std::sub_match representing an unmatched sub-expression (an empty subrange of the target sequence).

在您的情况下,regex_search 找到第一个匹配项,然后 match[0] 保存整个匹配文本,match[1] 将包含用第一个捕获组(第一个括号中的模式部分)捕获的文本等.但在这种情况下,您的正则表达式不包含捕获组.

In your case, regex_search finds the first match only and then match[0] holds the entire match text, match[1] would contain the text captured with the first capturing group (the fist parenthesized pattern part), etc. In this case though, your regex does not contain capturing groups.

在这里,您需要使用捕获机制,因为std::regex不支持查看后面.您使用了前瞻来检查紧跟当前位置的文本,并且您拥有的正则表达式没有按照您的想法行事.

Here, you need to use a capturing mechanism here since std::regex does not support a lookbehind. You used a lookahead that checks the text that immediately follows the current location, and the regex you have is not doing what you think it is.

因此,请使用以下代码:

#include <regex>
#include <string>
#include <iostream>
using namespace std;

int main() {
    std::regex expression(R"(am\s+(\d+))");
    std::smatch match;
    std::string what("I am 5 years old.");
    if (regex_search(what, match, expression))
    {
         cout << match.str(1) << endl;
    }
    return 0;
}

这里,模式是 am\s+(\d+)".它匹配 am、1+ 个空格,然后用 (\d+) 捕获 1 个或多个数字.在代码内部,match.str(1) 允许访问使用捕获组捕获的值.由于模式中只有一个 (...),一个捕获组,其 ID 为 1.因此,str(1) 返回捕获到该组中的文本.

Here, the pattern is am\s+(\d+)". It is matching am, 1+ whitespaces, and then captures 1 or more digits with (\d+). Inside the code, match.str(1) allows access to the values that are captured with capturing groups. As there is only one (...) in the pattern, one capturing group, its ID is 1. So, str(1) returns the text captured into this group.

原始字符串文字 (R"(...)") 允许使用单个反斜杠进行 regex 转义(如 \d\s 等).

The raw string literal (R"(...)") allows using a single backslash for regex escapes (like \d, \s, etc.).

这篇关于std::smatch 返回什么,你应该如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆