Difference between std::regex_match amp; std::regex_search?(std::regex_match amp; 之间的区别std::regex_search?)
问题描述
编写了以下程序以使用 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 expressione, and all of the character sequence[first,last)....Returnstrueif such a match exists,falseotherwise.
以上描述是针对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 expressione....Returnstrueif such a sequence exists,falseotherwise.
<小时>
在您的示例中,如果您将 regex 修改为 r{R"(.*?sd{2}s.*)"};regex_match 和 regex_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_match 和 regex_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.
这篇关于std::regex_match & 之间的区别std::regex_search?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::regex_match & 之间的区别std::regex_search?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
