2013-03-27 18 views
10

C++ 11'deki yeni std :: regex'in nasıl kullanıldığını öğrenmek için çalışın. Ama denediğim örnek, anlamıyorum bir regex_error istisnası atıyor.Bu neden C++ 11 std :: regex örneği bir regex_error istisnası atar?

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

ben derlemek ve bu gibi koşmak: İşte benim örnek kod

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

gdb backtrace bakınca atılan istisna regex_constants::error_brack olduğunu görüyoruz.

+2

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

+0

Hangi gcc sürümünü kullanıyorsunuz? – james82345

+1

@JamesEldridge 4.8.0 sürümünde bile hangi sürümün desteklenmediği std :: regex – Praetorian

cevap

4

İpucu için teşekkürler. G ++ içindeki normal ifadenin eksik olduğu konusunda hiçbir fikrim yoktu. Bu arada

, biz bu eski StackOverflow soruya bakın gerekecek sanırım:

C++: what regex library should I use?

+2

İlk gcc desteği gcc 4.9: http://stackoverflow.com/questions/23474121/what-part-of-regex-is-supported-by-gcc-4-9 – ACyclic

İlgili konular