regex_match和regex_search之间的区别? [英] Difference between regex_match and regex_search?

查看:1136
本文介绍了regex_match和regex_search之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用正则表达式尝试解答此问题,并且发现 regex_match 找到匹配项, regex_search 则不会。



以下程序是用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\\\
;
}

输出:



<$是我的假设,两个都应该匹配错误,我的假设,两个应该匹配错误,或者在GCC 4.7.1中有一个库的问题?

解决方案

您的正则表达式 (两个匹配,这是正确的)在VS 2012rc。



在g ++ 4.7.1 ( - std = gnu ++ 11),如果使用:




  • * FILE _(。+)_ EVENT\\.DAT。* regex_match 匹配,但 regex_search 不匹配。

  • 。*?FILE _(。+?)_ EVENT\\.DAT。* regex_match c> regex_search 匹配(O_o)。



所有变体应匹配,已由 betabandido 指出)。在g ++ 4.6.3 ( - std = gnu ++ 0x)中,行为与g ++ 4.7.1相同。



Boost(1.50)正确匹配一切两种模式品种。



摘要

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

关于你的模式,如果你一个点字符'。',那么你应该这样写(\\。)。您也可以使用非贪婪修饰符()减少回溯:

 。*?FILE _(。+?)_ EVENT\\.DAT。*


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?

解决方案

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

这篇关于regex_match和regex_search之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆