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