为什么这个C ++ 11 std :: regex示例抛出一个regex_error异常? [英] Why does this C++11 std::regex example throw a regex_error exception?

查看:2903
本文介绍了为什么这个C ++ 11 std :: regex示例抛出一个regex_error异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试学习如何在C ++ 11中使用新的std :: regex。但我的例子我试图抛出一个regex_error异常,我不明白。这里是我的示例代码:

Trying to learn how to use the new std::regex in C++11. But the example I tried is throwing a regex_error exception I don't understand. Here is my sample code:

#include <iostream>
#include <regex>

int main()
{
    std::string str = "xyzabc1xyzabc2xyzabc3abc4xyz";
    std::regex re( "(abc[1234])" ); // <-- this line throws a C++ exception

    // also tried to do this:
    // std::regex re( "(abc[1234])", std::regex::optimize | std::regex::extended );

    while ( true )
    {
        std::cout << "searching in " << str << std::endl;
        std::smatch match;
        std::regex_search( str, match, re );
        if ( match.empty() )
        {
            std::cout << "...no more matches" << std::endl;
            break;
        }
        for ( auto x : match )
        {
            std::cout << "found: " << x << std::endl;
        }
        str = match.suffix().str();
    }
    return 0;
}

我这样编译和运行:

g++ -g -std=c++11 test.cpp
./a.out
terminate called after throwing an instance of 'std::regex_error'
  what():  regex_error

看看gdb中的backtrace请参见异常抛出是 regex_constants :: error_brack

Looking at the backtrace in gdb, I see the exception thrown is regex_constants::error_brack.

推荐答案

提示。不知道g ++中的正则表达式代码是不完整的。

Thanks for the hint. Had no idea the regex code in g++ was incomplete.

在此期间,猜测我们必须参考这个旧的StackOverflow问题:

In the meantime, guess we'll have to refer to this old StackOverflow question:

C ++:我应该使用什么正则表达式库?

这篇关于为什么这个C ++ 11 std :: regex示例抛出一个regex_error异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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