Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

c++ - Difference between regex_match and regex_search?

I was experimenting with regular expression in trying to make an answer to this question, and found that while regex_match finds a match, regex_search does not.

The following program was compiled with g++ 4.7.1:

#include <regex>
#include <iostream>

int main()
{
    const std::string s = "/home/toto/FILE_mysymbol_EVENT.DAT";
    std::regex rgx(".*FILE_(.+)_EVENT\.DAT.*");
    std::smatch match;

    if (std::regex_match(s.begin(), s.end(), rgx))
        std::cout << "regex_match: match
";
    else
        std::cout << "regex_match: no match
";

    if (std::regex_search(s.begin(), s.end(), match, rgx))
        std::cout << "regex_search: match
";
    else
        std::cout << "regex_search: no match
";
}

Output:

regex_match: match
regex_search: no match

Is my assumption that both should match wrong, or might there a problem with the library in GCC 4.7.1?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your regex works fine (both match, which is correct) in VS 2012rc.

In g++ 4.7.1 (-std=gnu++11), if using:

  • ".*FILE_(.+)_EVENT\.DAT.*", regex_match matches, but regex_search doesn't.
  • ".*?FILE_(.+?)_EVENT\.DAT.*", neither regex_match nor regex_search matches (O_o).

All variants should match but some don't (for reasons that have been pointed out already by betabandido). In g++ 4.6.3 (-std=gnu++0x), the behavior is identical to g++ 4.7.1.

Boost (1.50) matches everything correctly w/both pattern varieties.

Summary:

                        regex_match      regex_search
 -----------------------------------------------------
 g++ 4.6.3 linux            OK/-               -
 g++ 4.7.1 linux            OK/-               -
 vs 2010                     OK                OK
 vs 2012rc                   OK                OK
 boost 1.50 win              OK                OK
 boost 1.50 linux            OK                OK
 -----------------------------------------------------

Regarding your pattern, if you mean a dot character '.', then you should write so ("\."). You can also reduce backtracking by using non-greedy modifiers (?):

".*?FILE_(.+?)_EVENT\.DAT.*"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...