Por que este exemplo C ++ 11 std :: regex gera uma exceção regex_error? [duplicado
Esta pergunta já tem uma resposta aqui:
O gcc 4.8 ou anterior é um buggy sobre expressões regulares? 3 respostasTentando aprender como usar o novo std :: regex no C ++ 11. Mas o exemplo que tentei está lançando uma exceção regex_error que não entendo. Aqui está o meu código de exemplo:
#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;
}
Compilo e corro assim:
g++ -g -std=c++11 test.cpp
./a.out
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Observando o backtrace em gdb, vejo a exceção lançada éregex_constants::error_brack
.