AlphaProgrammer AlphaProgrammer - 1 year ago 153
C++ Question

C++ boost regex date error

I am quite new to boost regex library.The following sample code is used to check if the entered date is following the

format.However,there seems to be an error in the regex. It always return's

  • I am running the console application on windows.

the regex was taken from 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;

return 0;

Answer Source

You're almost there; just add hyphens to your regex:


BTW, this won't parse dates before 2000 or after 2099. And there's no explicit end-of-string ($) at the end. Something more like:


...I think should make you good anywhere in recent centuries ;-)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download