c++ boost regex日期错误
C++ boost regex date error
我对增强正则表达式库很陌生。下面的示例代码用于检查输入的日期是否遵循YYYY-MM-DD
格式。然而,在正则表达式中似乎有一个错误。它总是返回false
。*
我正在windows上运行控制台应用程序。
*正则表达式取自此处
bool regexValidate(string teststring)
{
boost::regex ex("^(20\d{2})(\d{2})(\d{2})");
if (boost::regex_match(teststring, ex)) {
cout << "true";
return true;
}
else {
return false;
}
}
int main()
{
string teststr = "2016-05-15";
cout << teststr << " is ";
if (regexValidate( teststr)) {
cout << " valid!" << endl;
}
else {
cout << " invalid!" << endl;
}
system("PAUSE");
return 0;
}
你就快成功了;只需在regex中添加连字符:
"^(20\d{2})-(\d{2})-(\d{2})"
顺便说一句,这不会解析2000年之前或2099年之后的日期。并且在末尾没有显式的字符串结束符($)。更像是:
"^(\d{4})-(\d{2})-(\d{2})$"
…我想应该能让你在最近几个世纪的任何地方都表现出色;-)
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 无法在初始化时将"日期"转换为"int"作为错误
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- Visual 2015 上的 std::get_time 不会在错误的日期失败
- "日期":'class'类型重新定义:错误(日历.cpp文件错误)
- 该代码的主要错误是什么,用于确定n`天后日期
- 在 CLI 中转换日期时间time_t错误处理C++是否安全
- C++ <ctime> 打印错误的日期
- 这是Zeller程序的代码.除了1111年2月31日、1111年6月31日这一通常是错误的日期外,一切都很顺利
- NTP服务器总是返回相同的错误日期(窗口)
- 提升公历日期格式会产生错误的结果
- C++提升库 - 正确处理日期序列内存分配错误
- c++ to_string日期错误
- c++ boost regex日期错误
- Boost 1.53本地日期时间编译器错误使用-std=c++0x
- Visual Studio 2012中Boost posix日期和时间的编译错误