std::regex_match & 之间的区别std::regex_search?

2021-12-31 00:00:00 regex c++ c++11 visual-studio-2013 gcc4.9

编写了以下程序以使用 C++11 std::regex_match &std::regex_search.但是,使用第一种方法返回 false,第二种方法返回 true(预期).我阅读了文档并且已经存在与此相关的 SO 问题,但是我不明白这两种方法之间的区别以及何时应该使用它们中的任何一种?对于任何常见问题,它们都可以互换使用吗?

Below program has been written to fetch the "Day" information using the C++11 std::regex_match & std::regex_search. However, using the first method returns false and second method returns true(expected). I read the documentation and already existing SO question related to this, but I do not understand the difference between these two methods and when we should use either of them? Can they both be used interchangeably for any common problem?

regex_match 和 regex_search 的区别?

#include<iostream>
#include<string>
#include<regex>

int main()
{
    std::string input{ "Mon Nov 25 20:54:36 2013" };
    //Day:: Exactly Two Number surrounded by spaces in both side
    std::regex  r{R"(sd{2}s)"};
    //std::regex  r{"\s\d{2}\s"};
    std::smatch match;

if (std::regex_match(input,match,r)) {
        std::cout << "Found" << "
";
    } else {
        std::cout << "Did Not Found" << "
";
    }

    if (std::regex_search(input, match,r)) {
        std::cout << "Found" << "
";
        if (match.ready()){
            std::string out = match[0];
            std::cout << out << "
";
        }
    }
    else {
        std::cout << "Did Not Found" << "
";
    }
}

输出

Did Not Found

Found

 25 

在这种情况下,为什么第一个正则表达式方法返回 false?.regex 似乎是正确的,所以理想情况下两者都应该返回 true.我通过将 std::regex_match(input,match,r) 更改为 std::regex_match(input,r) 来运行上面的程序,发现它仍然返回 <代码>假.

Why first regex method returns false in this case?. The regex seems to be correct so ideally both should have been returned true. I ran the above program by changing the std::regex_match(input,match,r) to std::regex_match(input,r) and found that it still returns false.

有人能解释一下上面的例子,以及这些方法的一般用例吗?

Could somebody explain the above example and, in general, use cases of these methods?

推荐答案

regex_match 仅在整个输入序列匹配时返回 true,而 regex_search<即使只有一个子序列与 regex 匹配,/code> 也会成功.

regex_match only returns true when the entire input sequence has been matched, while regex_search will succeed even if only a sub-sequence matches the regex.

引自 N3337,

§28.11.2/2 regex_match [re.alg.match]

作用:判断正则表达式e和所有的字符序列[first,last).... 如果存在这样的匹配,则返回 true,否则返回 false.

§28.11.2/2 regex_match [re.alg.match]

Effects: Determines whether there is a match between the regular expression e, and all of the character sequence [first,last). ... Returns true if such a match exists, false otherwise.

以上描述是针对regex_match 重载的,它采用一对迭代器到要匹配的序列.其余的重载是根据这个重载定义的.

The above description is for the regex_match overload that takes a pair of iterators to the sequence to be matched. The remaining overloads are defined in terms of this overload.

对应的regex_search重载描述为

§28.11.3/2 regex_search [re.alg.search]

Effects: 判断[first,last)中是否存在与正则表达式e匹配的某个子序列.... 如果存在这样的序列,则返回 true,否则返回 false.

§28.11.3/2 regex_search [re.alg.search]

Effects: Determines whether there is some sub-sequence within [first,last) that matches the regular expression e. ... Returns true if such a sequence exists, false otherwise.

<小时>

在您的示例中,如果您将 regex 修改为 r{R"(.*?sd{2}s.*)"};regex_matchregex_search 都会成功(但匹配结果不仅仅是日期,而是整个日期字符串).


In your example, if you modify the regex to r{R"(.*?sd{2}s.*)"}; both regex_match and regex_search will succeed (but the match result is not just the day, but the entire date string).

现场演示 示例的修改版本,其中 regex_matchregex_search.

Live demo of a modified version of your example where the day is being captured and displayed by both regex_match and regex_search.

相关文章