【问题标题】:Why does this C++11 std::regex example throw a regex_error exception? [duplicate]为什么这个 C++11 std::regex 示例会引发 regex_error 异常? [复制]
【发布时间】:2013-03-27 23:32:53
【问题描述】:

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

#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中的回溯,我看到抛出的异常是regex_constants::error_brack

【问题讨论】:

标签: c++ regex c++11 g++


【解决方案1】:

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

与此同时,猜想我们将不得不参考这个旧的 StackOverflow 问题:

C++: what regex library should I use?

【讨论】:

猜你喜欢
  • 2016-12-29
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
  • 2013-07-27
  • 2017-01-18
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多