c++ - 无法使用regex c++ 11 std在Windows中运行使用minGW编译的程序

标签 c++ regex c++11 mingw

<分区>

这个程序编译正常,没有任何错误,但是当我运行这个程序时,它意外退出,说它遇到了一些问题。

使用 gdb 分析程序遇到了段错误。我对 gdb 了解不多,所以无法彻底检查是否有人可以重现问题并解释有帮助的错误。

还有我能做些什么来纠正这个问题。

#include<iostream>
#include<regex>
#include<string>
using namespace std;

int main()
{
   bool found;
   cmatch m;
try
{
    found = regex_search("<html>blah blah blah </html>",m,regex("<.*>.* </\\1"));
    cout<< found<<endl<<m.str();
}
catch(exception & e)
{

    cout<<e.what();
}

return 0;


}

最佳答案

您的反向引用需要引用一个组。

#include<iostream>
#include<regex>
#include<string>
using namespace std;

int main()
{
    bool found;
    cmatch m;
    try
    {
        found = regex_search("<html>blah blah blah </html>",m,regex("<(.*)>(.*)</\\1>"));
        cout << "***whole match***\n";
        cout << "found=" << found << endl;
        cout << m.str() << endl;

        cout << "\n*** parts ***" << endl;
        for (const auto& c : m) {
            cout << c << endl;
        }

    }
    catch(exception & e)
    {

        cout<<e.what() << endl;
    }

    return 0;


}

预期输出:

***whole match***
found=1
<html>blah blah blah </html>

*** parts ***
<html>blah blah blah </html>
html
blah blah blah 

关于c++ - 无法使用regex c++ 11 std在Windows中运行使用minGW编译的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31140635/

相关文章:

c++ - 如何获取脚本以在 C++ 中插入整个用户输入

c++ - 如何在参数中传递函数,然后从对象调用该函数

c++ - 如何防止此无锁堆栈函数中的未定义行为和 ABA 问题?

正则表达式 - 前瞻断言

c++ - 在 Stroustrup 的 PPP 书第 12 章的练习中正确使用 shared_ptr 和 make_shared

c++ - 编译器如何区分 "vector::insert"的两个变体?

c++ Singleton with map 原因 "violation reading location"

c++ - 使用 TCP 套接字处理客户端断开连接的最佳方法

java - 如何从与 Java 中的某个表达式匹配的字符串中获取值?

mysql - 从 MySQL 中的文本字段中选择仅数字模式