linux下,c++11的regex的问题

zilaishuichina 2018-03-13 06:17:48

int main(int argc, char* argv[])
{
try
{
std::string dateStr = "2018-03-13";
printf("111\n");
std::regex dateReg("\d{4}-\d{2}-\d{2}");
//std::regex dateReg("[0-9]{4}-[0-9]{2}-[0-9]{2}");
printf("222\n");
if (std::regex_match(dateStr, dateReg))
{
printf("true\n");
}
else
{
printf("false\n");
}
}
catch (const std::regex_error& err)
{
printf("%d, %s\n", err.code(), err.what());
}

return 0;
}


就是想判断一个输入的字符串,是否是一个符合yyyy-mm-dd格式的日期

问题1:
当我使用std::regex dateReg("[0-9]{4}-[0-9]{2}-[0-9]{2}")(就是我注释掉的那一行的时候),输出的是:
111
4, regex_error
也就是说222那一行是没有打印出来的,说明在构造regex对象的时候抛异常了。
所以:gcc的regex是不是没有全部支持所有的正则元字符?还是我构造的有问题?
我的gcc版本 gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)

问题2:
改成使用std::regex dateReg("\d{4}-\d{2}-\d{2}"),输出的是:
111
222
false
就是说,不抛异常了,构造regex对象没问题了,但是没有匹配成功
所以:这个正则应该怎么写?

ps:vs2017下面都是正常的,不管哪种表达式,都能够匹配成功,输出true
...全文
815 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
缘来是梦 2018-03-19
  • 打赏
  • 举报
回复
gcc的问题, 升级一下吧. 现在都到7点几了
HaoYuan 2018-03-15
  • 打赏
  • 举报
回复
要看gcc的哪个版本,gcc早期的时候没有实现regex,只有声明,貌似到4,9以后的版本才支持了
paschen 版主 2018-03-14
  • 打赏
  • 举报
回复
这样啊,我是看的VS上是extended,可能你的环境下不是,我没在linux下试过,你可以看下能不能跟踪进标准库代码,看具体原因
zilaishuichina 2018-03-14
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
err.code()为4应该是std::regex_constants::extended,即使用了扩展的POSIX语法 具体描述可看: http://en.cppreference.com/w/cpp/regex/basic_regex http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04
error code 是4 , 不是extended,是 error_brack,the expression contains mismatched square brackets ('[' and ']') ,左右 中括号不匹配,所以这个错误码实在无法理解。

std::regex_constants::error_type
  C++  Regular expressions library 
Defined in header <regex>
typedef /*implementation defined*/ error_type;
(since C++11)
constexpr error_type error_collate = /*unspecified*/;
constexpr error_type error_ctype = /*unspecified*/;
constexpr error_type error_escape = /*unspecified*/;
constexpr error_type error_backref = /*unspecified*/;
constexpr error_type error_brack = /*unspecified*/;
constexpr error_type error_paren = /*unspecified*/;
constexpr error_type error_brace = /*unspecified*/;
constexpr error_type error_badbrace = /*unspecified*/;
constexpr error_type error_range = /*unspecified*/;
constexpr error_type error_space = /*unspecified*/;
constexpr error_type error_badrepeat = /*unspecified*/;
constexpr error_type error_complexity = /*unspecified*/;
constexpr error_type error_stack = /*unspecified*/;
(since C++11) 
(until C++17)
inline constexpr error_type error_collate = /*unspecified*/;
inline constexpr error_type error_ctype = /*unspecified*/;
inline constexpr error_type error_escape = /*unspecified*/;
inline constexpr error_type error_backref = /*unspecified*/;
inline constexpr error_type error_brack = /*unspecified*/;
inline constexpr error_type error_paren = /*unspecified*/;
inline constexpr error_type error_brace = /*unspecified*/;
inline constexpr error_type error_badbrace = /*unspecified*/;
inline constexpr error_type error_range = /*unspecified*/;
inline constexpr error_type error_space = /*unspecified*/;
inline constexpr error_type error_badrepeat = /*unspecified*/;
inline constexpr error_type error_complexity = /*unspecified*/;
inline constexpr error_type error_stack = /*unspecified*/;
(since C++17)
The error_type is a type that describes errors that may occur during regular expression parsing.

Constants
Constant	Explanation
error_collate	the expression contains an invalid collating element name
error_ctype	the expression contains an invalid character class name
error_escape	the expression contains an invalid escaped character or a trailing escape
error_backref	the expression contains an invalid back reference
error_brack	the expression contains mismatched square brackets ('[' and ']')
error_paren	the expression contains mismatched parentheses ('(' and ')')
error_brace	the expression contains mismatched curly braces ('{' and '}')
error_badbrace	the expression contains an invalid range in a {} expression
error_range	the expression contains an invalid character range (e.g. [b-a])
error_space	there was not enough memory to convert the expression into a finite state machine
error_badrepeat	one of *?+{ was not preceded by a valid regular expression
error_complexity	the complexity of an attempted match exceeded a predefined level
error_stack	there was not enough memory to perform a match
paschen 版主 2018-03-13
  • 打赏
  • 举报
回复
err.code()为4应该是std::regex_constants::extended,即使用了扩展的POSIX语法 具体描述可看: http://en.cppreference.com/w/cpp/regex/basic_regex http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧