【问题标题】:Regex C Help on escape character正则表达式 C 帮助转义字符
【发布时间】:2013-06-01 17:16:51
【问题描述】:

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

我收到此错误,但由于某种原因不是我的异常:

此应用程序已请求运行时以不寻常的方式终止它。 请联系应用程序的支持团队以获取更多信息。 在抛出 '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;
}

【问题讨论】:

  • &lt;regex&gt; 在 gcc 中的支持是 far from complete。如果你需要正则表达式,你应该改用 Boost。
  • C++11 正则表达式是否支持[[:cntrl:]]
  • 哇,我什至不知道 regex 仍然是 gcc 的粗略形式。那么,Windows 的 Visual Studio 编译器是否已完全实现?我会坚持使用 boost。
  • @HeroofCode Visual Studio 2012 确实支持正则表达式,但缺少其他几个 C++11 语言功能。
  • @Praetorian -- 不是正则表达式 TR1(大约 2003 年)吗?

标签: c++ regex c++11


【解决方案1】:

FWIW,您的 C++ 代码没有任何问题,并且在我的 MacBook 上使用 Clang 和 libc++ 可以完美运行。

如上面的 cmets 所示,&lt;regex&gt; 是 LLVM 项目的 libc++ 但has never worked properly in the GNU project's libstdc++ 中工作的功能之一。如果您的平台可用,您可以尝试切换到 libc++。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多