I want to check date format in a string, so I use std::regex in c++;
I goggled some demo, but they all failed to me, includes:
#include <iostream>
#include <regex>
using namespace std;
int main() {
string text = "future2018-7-12";
// regex pattern("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}"); // these three all failed
// regex pattern("[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}");
std::regex pattern("\\b\\d{4}[-]\\d{2}[-]\\d{2}\\b");
smatch results;
if ( regex_match(text, results, pattern) ) {
smatch::iterator it = results.begin();
int i = 0;
for(; it != results.end(); ++it, ++i)
cout<<i<<": "<<*it<<endl;
} else {
cout << "match failed: " <<text<< endl;
}
return 0;
}
my compile command is:
g++ -std=c++11 reg.cpp -o reg
execute command is:
./reg
my machine is:
centos 7 in vmware
they all crashed with the error:
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
that confuses me, because i can only find these demo about regex.
So, what wrong with my code?