2013-03-27 7 views
10

Versuchen zu lernen, wie man den neuen std :: regex in C++ benutzt 11. Aber das Beispiel, das ich versuchte, wirft eine regex_error Ausnahme, die ich nicht verstehe. Hier ist mein Beispielcode:Warum wirft dieses C++ 11 std :: regex-Beispiel eine regex_error-Ausnahme?

#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; 
} 

ich kompilieren und wie folgt ausführen:

g++ -g -std=c++11 test.cpp 
./a.out 
terminate called after throwing an instance of 'std::regex_error' 
    what(): regex_error 

Mit Blick auf die Backtrace in GDB, sehe ich die ausgelöste Ausnahme regex_constants::error_brack ist.

+2

[g ++ libstdC++ nicht unterstützt std :: regex] (http://gcc.gnu.org /onlinedocs/libstdc++/manual/status.html#status.iso.200x) –

+0

Welche Version von gcc verwendest du? – james82345

+1

@JamesEldridge Egal welche Version, auch 4.8.0 unterstützt nicht std :: regex – Praetorian

Antwort

4

Danke für den Hinweis. Hatte keine Ahnung, dass der Regex-Code in g ++ unvollständig war.

In der Zwischenzeit denke, wir werden auf diese alte Frage Stackoverflow beziehen müssen:

C++: what regex library should I use?

+2

Erste gcc-Unterstützung ist in gcc 4.9: http://stackoverflow.com/questions/23474121/what-part-of-regex-is-supported-by-gcc-4-9 – ACyclic

Verwandte Themen