正则表达式C帮助转义字符

时间:2013-06-05 20:44:52

标签: c++ regex c++11

我无法从字符串中提取令牌值:“JOIN #ROOM \ r \ n” 我正在使用以下参数在Mingw64上编译我的代码: g ++ tregex.cpp -o tregex.exe -std = gnu ++ 11

我收到此错误,但出于某种原因不是我的例外:

  

此应用程序已请求Runtime以不寻常的方式终止它。   有关更多信息,请联系应用程序的支持团队。   在抛出'std :: regex_error'的实例后终止调用     what():regex_error

这是我的代码:

#include <regex>
#include <string>
#include <iostream>
using namespace std;

//Tregex.cpp

int main(void) {
    regex rgx("[[:cntrl:]]");
    string str = "JOIN  #ROOM\r\n";
    smatch match;
    try{
        if(regex_search(str, match, rgx))
            for(auto token:match) cout << token <<"\n";
        cout<< endl;
    }
    catch(regex_error & e){
        if( e.code() == regex_constants::error_escape )
            cerr << "invalid escape character \n";
        else if( e.code() == regex_constants::error_stack )
            cerr << "regular expression is not big enough\n";
        else
            cerr << "exception caught: "<< e.what()<<"\n";
    }
    cin.get();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

FWIW,您的C ++代码没有任何问题,并且在MacBook上使用Clang和libc++可以很好地工作。

如上面的评论所示,<regex>是LLVM项目的libc ++中的一个功能,但是has never worked properly in the GNU project's libstdc++。如果它适用于您的平台,您可以尝试切换到libc ++。