c++ - Difference between std::regex_match & std::regex_search? -
below programme has been written fetch "day" info using c++11 std::regex_match & std::regex_search. however, using first method returns false
, sec method returns true
(expected). read documentation , existing question related this, not understand difference between these 2 methods , when should utilize either of them? can both used interchangeably mutual problem?
difference between regex_match , regex_search?
#include<iostream> #include<string> #include<regex> int main() { std::string input{ "mon nov 25 20:54:36 2013" }; //day:: 2 number surrounded 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
why first regex method returns false
in case?. regex
seems right ideally both should have been returned true
. ran above programme changing std::regex_match(input,match,r)
std::regex_match(input,r)
, found still returns false.
could explain above illustration and, in general, utilize cases of these methods?
regex_match
returns true
when entire input sequence has been matched, while regex_search
succeed if sub-sequence matches regex
.
quoting n3337,
§28.11.2/2 regex_match
[re.alg.match] effects: determines whether there match between regular look e
, , all of character sequence [first,last)
. ...
returns true
if such match exists, false
otherwise.
the above description regex_match
overload takes pair of iterators sequence matched. remaining overloads defined in terms of overload.
the corresponding regex_search
overload described
§28.11.3/2 regex_search
[re.alg.search] effects: determines whether there some sub-sequence within [first,last)
matches regular look e
. ...
returns true
if such sequence exists, false
otherwise.
in example, if modify regex
r{r"(.*?\s\d{2}\s.*)"};
both regex_match
, regex_search
succeed (but match result not day, entire date string).
live demo of modified version of illustration day beingness captured , displayed both regex_match
, regex_search
.
c++ regex c++11 visual-studio-2013 gcc4.9
No comments:
Post a Comment