regex_error(error_stack)C ++

时间:2016-07-08 17:18:27

标签: c++ regex visual-studio c++11

我遇到了运行程序时遇到regex_error(error_stack)的问题。这有时只会发生(取决于输入字符串)。我在尊重引号的同时使用逗号分隔分割:

我的正则表达式是:

",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"

这是代码:

void split( const std::string &s, std::vector<std::string> &elems, const std::string &rgx_str ) {
    std::regex rgx( rgx_str );

    std::sregex_token_iterator iter( s.begin(), s.end(), rgx, -1 );
    std::sregex_token_iterator end;

    while ( iter != end ) {
        std::string tmp = *iter;
        elems.push_back( trim( tmp ) );
        ++iter;
    }
}

错误发生在创建iter(...)的行上(实际上,错误深埋在正则表达式代码中,但这是我使用的代码的最后一部分)。我正在使用MS Visual Studio 2015 Update 3.我在regexr.com上测试了正则表达式,它运行正常。它经常失败的字符串在下面(它是文件中的一行):

"1942","96 Level Acc #2","",,,0,1,0,0,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,0,2.00000004008175e+021,2.00000004008175e+021,0,0,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,0,0,0,0,0,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2.00000004008175e+021,2.00000004008175e+021,0,0,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,0,0,2.00000004008175e+021,0,0,2.00000004008175e+021,0,0,0,0,0,0,0,0,0,0,0,0,2.00000004008175e+021,0,0,0,0,0,0,0,2.00000004008175e+021,0,0,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,2.00000004008175e+021,0,2.00000004008175e+021,0,0,0,0,0,0,0,0,"DEVELOPMENT","<ND>  ","<ND>","<AUTO>","<New>","<ND>","<ND>","<ND>","<ND>","<ND>","<ND>","<ND>","<Not defined>","<ND>","<ND>","<AUTO>","< New >","< ND >","4.5","<ND>","<ND>","<ND>","<M>","<In Schedule>","<Not defined>","<Not defined>","<LHD 9 (Manned)>","<Operating>","","96","00","00","","","96-0000-ST","9150 R/B (ORE)","","B4 96-9150 TRK","","","MDB5","","-","","","95 TO 96 COB #4 MINE","","0","0","0","0","0","0","96","","","",Fixed Duration,6.4dtm/w,0d,As Soon As Possible,,,,0,,0,,"<Project>",Latest,Combined,Task,1,0,"","",0,0mi,""

我看不出正则表达式或我想要匹配的字符串有任何问题。似乎sregex_token_iterator失败了。我已经尝试将堆栈增加到10MB,但它没有帮助。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

由于某种原因,堆栈溢出,我不知道为什么。我试图从调用堆栈调试一点但它在不同阶段失败。我很快就会调试它。也许有人可以用更好的方式来解释它。

调用堆栈永远不会结束,我已经检查了几个失败的情况。但似乎它与字符串或正则表达式无关,而是内部的迭代器解析进入一个巨大的循环(可能是因为大字符串或可能是某些字符解析,有点无限循环)由于某种原因

调用堆栈充满了这些::

regex1.exe!std::_Matcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > >,char,std::regex_traits<char>,std::_String_const_iterator<std::_String_val<std::_Simple_types<char> > > >::_Match_pat(std::_Node_base * _Nx) Line 4756    C++

但下面是一个有效的解决方案::

void split(const  std::string &s, std::vector<std::string> &elems, const std::string &rgx_str) {
    std::regex rgx(rgx_str);
    std::smatch mat;
    std::regex rgxx(",");
    string s1(s);

    while (std::regex_search(s1, mat, rgxx))
    {
        elems.push_back(mat.prefix());
        s1 = mat.suffix();
    }
}

如果这可以解决您的问题,请告诉我?