2013-03-27 6 views
10

सी ++ 11 में नए std :: regex का उपयोग करने का तरीका सीखने की कोशिश कर रहा है। लेकिन मैंने जिस उदाहरण की कोशिश की वह एक regex_error अपवाद फेंक रहा है जिसे मैं समझ नहीं पा रहा हूं।यह सी ++ 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; 
} 

मैं संकलन और इस तरह चलाएँ:: यहाँ मेरी नमूना कोड है

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 है।

+2

(http://gcc.gnu.org [जी ++ libstdC++ std :: regex का समर्थन नहीं करता] /onlinedocs/libstdc++/manual/status.html#status.iso.200x) –

+0

आप किस जीसीसी का उपयोग कर रहे हैं? – james82345

+1

@JamesEldridge कोई फर्क नहीं पड़ता कि संस्करण क्या है, यहां तक ​​कि 4.8.0 std :: regex – Praetorian

उत्तर

4

संकेत के लिए धन्यवाद। नहीं पता था कि जी ++ में रेगेक्स कोड अपूर्ण था।

इस बीच में, अनुमान है कि हम इस वर्ष StackOverflow सवाल का उल्लेख करना होगा:

C++: what regex library should I use?

+2

पहला जीसीसी समर्थन जीसीसी 4.9 में है: http://stackoverflow.com/questions/23474121/what-part-of-regex-is-supported-by-gcc-4-9 – ACyclic

संबंधित मुद्दे