Próba nauczenia się używania nowego std :: regex w C++ 11. Ale przykład, który próbowałem, rzuca wyjątek regex_error, którego nie rozumiem. Oto mój przykładowy kod:Dlaczego ten C++ 11 std :: regex przykład rzuca wyjątek 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;
}
skompilować i uruchomić tak:
g++ -g -std=c++11 test.cpp
./a.out
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Patrząc na zapis w gdb, widzę wyjątek rzucony jest regex_constants::error_brack
.
[g ++ libstdC++ nie obsługuje std :: regex] (http://gcc.gnu.org /onlinedocs/libstdc++/manual/status.html#status.iso.200x) –
Jakiej wersji GCC używasz? – james82345
@JamesEldridge Nie ma znaczenia, która wersja, nawet 4.8.0, nie obsługuje std :: regex – Praetorian