I try to write a program to test regular expression in C++, so I read and following the standard example, my code is following:
#include <stdio.h>
#include <regex>
#include <string>
using namespace std;
char var1[10] = "12345";
string var2 = "12345";
int main()
{
if (regex_match( var1, regex( "\\d+" ) ) ){
printf("var1 match\n");
}else{
printf("var1 not match\n");
}
if (regex_match( var2, regex( "\\d+" ) ) ){
printf("var2 match\n");
}else{
printf("var2 not match\n");
}
}
Then I run command like following for compile and run:
g++ retest.cpp -o retest -std=c++0x
./retest
But it show me following error:
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted
I modified the code and finally found that error cause regular string in regex(). But when I write same code in my OSX, it can run and show me the correct result! I am so confused that why I use the standard C++ library, but different result in CentOS and OSX? How can I solve this problem? Thanks!