c++ - g++ 和 VS2012 中的正则表达式

标签 c++ regex visual-studio-2012 g++

#include <iostream>
#include <string>
#include <regex>

using namespace std;

void Test(const char* str, const char* regExpression)
{
    regex rx(regExpression);
    bool match = regex_match(str, rx);
    bool search = regex_search(str, rx);

    cout << "String: " << str << "  expression: " << regExpression <<
        "   match: " << (match ? "yes" : "no ") <<
        "   search: " << (search ? "yes" : "no ")  << endl;
}


int main()
{
    Test("a", "a");
    Test("a", "abc");
    return 0;
}

g++ 中的结果:

String: a  expression: a   match: yes   search: no 
String: a  expression: abc   match: no    search: no 

VS2012 中的结果:

String: a  expression: a   match: yes   search: yes
String: a  expression: abc   match: no    search: no

什么是正确的结果?另外,regex_match 和 regex_search 有什么区别?

最佳答案

VS2012 结果是对的。 _match 检查您的字符串是否与表达式匹配,_search 检查您的字符串的某些子字符串是否与表达式匹配。

"a""a" 的任何子字符串都不匹配表达式 "abc"

(我找不到相关的 SO 问题,但已知 gcc(而不是 libstdc++ 的)正则表达式实现存在错误且不完整。)

关于c++ - g++ 和 VS2012 中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15185397/

相关文章:

javascript - 正则表达式 - 仅数字、字符和空格

c++ - 有没有办法在 Visual Studio Express '12 项目中编译和运行单个 .cpp 文件?

visual-studio - 如果多个文件具有相同的名称,Visual Studio 断点会在错误的源文件(或同时多个文件)中中断

c++ - 在 dll 中使用全局变量以供以后在 Labview 中使用

c++ - STL图表现?

c++ - 无法从具有相同类型的两个来源之一创建 std::string

python - 使用正则表达式捕获组 (Python)

java - 国际电话的正则表达式

windows-8 - 使用 Windows 8 和 vs2012 的开发人员的最佳 VirtualBox 设置

C++ Linux 获取CPU标称频率