std :: regex_match& std :: regex_search? [英] Difference between std::regex_match & 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?
#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"(\s\d{2}\s)"};
//std::regex r{"\\s\\d{2}\\s"};
std::smatch match;
if (std::regex_match(input,match,r)) {
std::cout << "Found" << "\n";
} else {
std::cout << "Did Not Found" << "\n";
}
if (std::regex_search(input, match,r)) {
std::cout << "Found" << "\n";
if (match.ready()){
std::string out = match[0];
std::cout << out << "\n";
}
}
else {
std::cout << "Did Not Found" << "\n";
}
}
输出
Output
Did Not Found
Found
25
为什么在这种情况下,第一个regex方法返回 false
? regex
似乎是正确的,因此理想情况下两个都应该返回 true
。我通过改变 std :: regex_match(input,match,r)
到 std :: regex_match(input,r) code>并发现它仍然返回
false。
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
code> true ,而 regex_search
会成功,即使只有一个子序列匹配 regex
。
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]
/ em>确定正则表达式e
和所有字符序列之间是否匹配[first,last] code>
。... 如果存在此类匹配,则返回
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
.alg.search]
效果:确定中是否有 ,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(。*?\s\d {2} \s。*)};
regex_match
和 regex_search
将成功(但匹配结果不仅仅是日期,而是整个日期字符串)。
In your example, if you modify the regex
to r{R"(.*?\s\d{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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!