【发布时间】:2014-11-04 15:33:29
【问题描述】:
我正在尝试使用 Regex 库工具从文本文件中提取双精度和整数参数。这是捕获我收到的“std::regex_error”消息的最小代码:
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string My_String = "delta = -002.050";
std::smatch Match;
std::regex Base("/^[0-9]+(\\.[0-9]+)?$");
std::regex_match(My_String,Match,Base);
std::ssub_match Sub_Match = Match[1];
std::string Sub_String = Sub_Match.str();
std::cout << Sub_String << std::endl;
return 0;
}
我对 Regex 库不太熟悉,也找不到任何立即有用的东西。知道是什么原因导致此错误消息吗?为了编译我的代码,我使用启用了 -std=c++11 的 g++。但是,我确信问题不是由我的 g++ 编译器引起的,正如the answers given to this earlier question 中所建议的那样(我试过several g++ compilers here)。
我希望从字符串“delta = -002.050”中得到“-002.050”,但我得到了:
在抛出 'std::regex_error' 的实例后调用终止 什么():正则表达式错误 中止
【问题讨论】:
-
到底是什么问题,你期望的输出是什么,你得到了什么?
-
谢谢。我刚刚编辑了我的帖子。