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)
....
Returnstrue
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 expressione
....
Returnstrue
if such a sequence exists,false
otherwise.
<小时>
在您的示例中,如果您将 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?
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01