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
...全文
382 点赞 收藏 5
写回复
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
回复 点赞
发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告