Difference between regex_match and regex_search?

▼魔方 西西 提交于 2019-12-17 16:35:15

问题


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\n";
    else
        std::cout << "regex_match: no match\n";

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

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?


回答1:


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.*"



回答2:


Assuming that C++ and Boost Regex have a similar structure and functionality, the difference between regex_match and regex_search is explained here:

The regex_match() algorithm will only report success if the regex matches the whole input, from beginning to end. If the regex matches only a part of the input, regex_match() will return false. If you want to search through the string looking for sub-strings that the regex matches, use the regex_search() algorithm.




回答3:


Looking through the latest libstdc++ source code for regex_search, you will find:

* @todo Implement this function.

Unfortunately this is not the only TODO item left. GCC's <regex> implementation is currently incomplete. I recommend using Boost or Clang and #ifdef the code until GCC has caught up.

(This has neither been fixed in the 4.8 branch.)




回答4:


I tried to use the regex library in C++11 and I ran into many problems (both using g++ 4.6 and 4.7). Basically, the support is either not there or there is only partial support. That is true even for the SVN version. Here you have a link describing the current status for the SVN version of libstdc++.

So, for the time being, I guess the best option is to continue using Boost.Regex.

Alternatively, you can try to use libc++. According to this document, support for regular expressions is complete.



来源:https://stackoverflow.com/questions/11628047/difference-between-regex-match-and-regex-search

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!