【发布时间】: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。
【问题讨论】:
-
你用的是什么版本的gcc?
-
@JamesEldridge 不管什么版本,连4.8.0都不支持std::regex
-
@Praetorian 是的,你是对的,当前状态是 N:gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
-
总是有 boost 正则表达式:boost.org/doc/libs/1_53_0/libs/regex/doc/html/index.html