为什么这个正则表达式抛出'std :: regex_error'实例并生成coredump?

时间:2019-06-18 09:40:07

标签: c++ regex c++11 gcc

我正在学习c ++正则表达式。 下面的代码可以通过g ++ 4.8.5编译,但是当我运行它时,程序会抛出异常和核心转储:

$ ./17.3
terminate called after throwing an instance of 'std::regex_error'
  what():  regex_error
Aborted (core dumped)

gdb显示第12行:regex r(pattern, regex_constants::ECMAScript);

我将代码放入http://cpp.sh/,该代码可以正确运行并获得正确的结果。

我的编译器太旧了吗? 但是其他使用g ++ 4.8.5编译的正则表达式的程序可以正常运行, 谁能告诉我为什么?非常感谢。

#include <regex>                                                                                    
#include <string>                                                                                   
#include <iostream>                                                                                 

using namespace std;                                                                                

int main()                                                                                          
{                                                                                                   
    string pattern("[^c]ei");                                                                       
    pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";                                                                                                                                                                                                                                   

    regex r(pattern, regex_constants::ECMAScript);                                                  
    smatch results;                                                                                 

    string test_str = "receipt freind theif receive";                                               

    try {                                                                                           
        if (regex_search(test_str, results, r)) {                                                   
            cout << results.str() << endl;                                                          
        }                                                                                           
    } catch (regex_error e) {                                                                       
        cout << e.what() << "\ncode: " << e.code() << endl;                                         
    }                                                                                               
} 

0 个答案:

没有答案
相关问题