regex_match 和 regex_search 之间的区别?

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

问题描述

我正在尝试使用正则表达式来回答这个问题,并且发现虽然 regex_match 找到匹配项,但 regex_search 没有.

以下程序是用g++ 4.7.1编译的:

#include <regex>#include <iostream>主函数(){const std::string s = "/home/toto/FILE_mysymbol_EVENT.DAT";std::regex rgx(".*FILE_(.+)_EVENT\.DAT.*");std::smatch 匹配;if (std::regex_match(s.begin(), s.end(), rgx))std::cout <<正则表达式匹配:匹配
";别的std::cout <<"regex_match: 不匹配
";if (std::regex_search(s.begin(), s.end(), match, rgx))std::cout <<"regex_search: 匹配
";别的std::cout <<"regex_search: 不匹配
";}

输出:

<上一页>正则表达式匹配:匹配regex_search: 不匹配

我的假设是两者都应该匹配错误,还是 GCC 4.7.1 中的库有问题?

解决方案

您的正则表达式 工作正常(两者都匹配,这是正确的)在 VS 2012rc 中.

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

  • ".*FILE_(.+)_EVENT\.DAT.*", regex_match 匹配,但 regex_search 不匹配.
  • ".*?FILE_(.+?)_EVENT\.DAT.*"regex_matchregex_search 都不匹配 (O_o).

所有变体都应该匹配,但有些不匹配(原因已由 betabandido 指出).在 g++ 4.6.3 (-std=gnu++0x) 中,行为与 g++ 4.7.1 相同.

Boost (1.50) 正确匹配所有内容 两种模式变体.

总结:

<上一页>正则表达式匹配正则表达式搜索-----------------------------------------------------g++ 4.6.3 linux OK/- -g++ 4.7.1 linux OK/- -与 2010 年相比 好的 好的vs 2012rc OK OK提升 1.50 胜利 OK OKboost 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
";
    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?

解决方案

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 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)