Почему этот пример C ++ 11 std :: regex вызывает исключение regex_error? [Дубликат]
На этот вопрос уже есть ответ:
Gcc 4.8 или более ранний глючит в регулярных выражениях? 3 ответаПопытка научиться использовать новый стандарт std :: regex в C ++ 11. Но пример, который я попробовал, вызывает исключение 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
.