【问题标题】:C++ boost regex date errorC ++提升正则表达式日期错误
【发布时间】:2017-01-26 00:38:33
【问题描述】:

我对提升正则表达式库很陌生。以下示例代码用于检查输入的日期是否遵循YYYY-MM-DD 格式。但是,正则表达式似乎有错误。它总是返回'sfalse。 *

  • 我正在 Windows 上运行控制台应用程序。

* 正则表达式取自here

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;
 }

【问题讨论】:

    标签: c++ regex boost


    【解决方案1】:

    你快到了;只需在您的正则表达式中添加连字符:

    "^(20\\d{2})-(\\d{2})-(\\d{2})"
    

    顺便说一句,这不会解析 2000 年之前或 2099 年之后的日期。并且末尾没有明确的字符串结尾 ($)。更像是:

    "^(\\d{4})-(\\d{2})-(\\d{2})$"
    

    ...我认为在最近几个世纪里应该让你在任何地方都变得更好;-)

    【讨论】:

    • 谢谢!工作正常应包括字符串结尾($)
    猜你喜欢
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多