Difference between std::regex_match & std::regex_search?
Asked Answered
P

2

42

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?

Difference between regex_match and 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"(\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 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?

Possessive answered 2/11, 2014 at 5:6 Comment(0)
B
41

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.

Breezy answered 2/11, 2014 at 5:21 Comment(3)
Thanks for the explanation. Could you please explain about the why we needed to changed from match[0] to match[1] to get the exact result in both the cases?. I mean it is about std::smatch uses understanding.Possessive
@MantoshKumar I added parentheses around the date field (\d{2}) to create a capture group. From the match_results documentation, match[0] always returns the entire matched expression, while match[1] returns the first sub-match and so on. In this case, we only had one capture group, for the day, and so that is stored in the first sub-match.Breezy
From a performance standpoint, I bet regex_match is computationally much faster than regex_search, on average, due to the ability to return false immediately when a match fails. In contrast, regex_search is going to check if substrings match the pattern, so in most cases, if not all cases, regex_search will be slower. So take that into consideration when choosing between these functions.Soundproof
S
21

It's very simple. regex_search looks through the string to find if any portion of the string matches the regex. regex_match checks if the whole string is a match for the regex. As a simple example, given the following string:

"one two three four"

If I use regex_search on that string with the expression "three", it will succeed, because "three" can be found in "one two three four"

However, if I use regex_match instead, it will fail, because "three" is not the whole string, but only a part of it.

Sulfonmethane answered 2/11, 2014 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.