C++11 match_regex与简单模式不匹配
C++11 match_regex doesn't match a simple pattern
我在C++使用新的正则表达式库时遇到了一些问题。下面是一个简单的示例:
#include<regex>
#include<string>
#include<iostream>
using namespace std;
int main(){
string text = "123.456";
string pattern = "[0-9]+\.[0-9]+";
try{
cout << (regex_match(text, regex(pattern, regex_constants::extended)) ? "Passn" : "Failn");
}catch(...){
cout << "Fail (bad regex)n";
}
return 0;
}
问题是,无论我使用哪种类型的匹配(简单,扩展,grep,egrep,awk等),它总是返回false。如果我使用"regex_constants::simple",它会抛出异常,因为不支持括号表达式,但我检查了规范,它应该可以正常工作"regex_constants::extended"。
结果如下:
rhobincu@daneel:~/work$ g++ -std=c++11 test.cpp -o test
rhobincu@daneel:~/work$ ./test
Fail
知道我做错了什么吗?
编辑:这可能也是有用的信息:
rhobincu@daneel:~/work$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
regex
在GCC的libstdc++中尚不受支持,当前状态。
您只需将std::regex
替换为boost::regex
,它就可以编译并正常工作。
相关文章:
- 在c++中用vector填充一个简单的动态数组
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 为什么在保护模式下继承升级不起作用
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在全屏模式下(在OpenGL中)使背景透明
- 如何在BST的这个简单递归实现中消除警告
- 模型测试 + 简单表模式 = 父测试失败
- 最简单的事件设计模式是什么
- 简单的内核模式驱动程序
- 这个简单但有用的模式是不是太简单了,不可能成为一个真正的模式
- C++11 match_regex与简单模式不匹配
- 为什么他们在原型模式中这么说 - 用于在需要新对象时简单地复制原始对象
- 简单的子类模板模式
- 如何使用标准库(包括boost)实现简单的字符串模式匹配
- 简单的OpenCV命令可以在调试模式下工作,但不能在发布模式下工作
- 在一个简单的策略模式的bug
- 与Visual Studio 2010中的MD相反,无法在MT模式下编译简单的Qt程序
- 设计模式-用于简单静态数据的C++架构