2013-03-27 4 views
10

C++で新しいstd :: regexを使用する方法を学ぶ11。しかし、私が試した例では、私は理解していないregex_error例外をスローしています。ここに私のサンプルコードは次のとおりです。なぜこの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; 
} 

私はコンパイルして、次のように実行します:gdbの中でバックトレースを見ると

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

、私がスローされた例外がregex_constants::error_brackある参照してください。ヒントの

+2

(http://gcc.gnu.org [G ++のlibstdC++のstd ::正規表現をサポートしていません] /onlinedocs/libstdc++/manual/status.html#status.iso.200x) –

+0

あなたが使用しているGCCのバージョンは何? – james82345

+1

@JamesEldridgeも4.8.0は、STDをサポートしていないどのバージョン、問題ではありません::正規表現 – Praetorian

答えて

4

感謝。 g ++の正規表現コードが不完全であるという考えはありませんでした。一方

、私たちはこの古いStackOverflowの質問を参照する必要がありますね:

C++: what regex library should I use?

+2

まずgccのサポートがGCC 4.9で、次のとおりです。http:/ /stackoverflow.com/questions/23474121/what-part-of-regex-is-supported-by-gcc-4-9 – ACyclic

関連する問題