Difference between std::regex_match & std::regex_search?

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.

Quoting from N3337,

§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.

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.

The corresponding regex_search overload is described as

§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.


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).

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

Leave a Comment