Issue
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!
Solution
CentOS has quite old GCC package. You can check it details in http://distrowatch.com/table.php?distribution=centos
On my CentOS 7 machine says,
# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
and, <regex>
was implemented in gcc 4.9.0. https://stackoverflow.com/a/12665408/3627572
Answered By - Byoungchan Lee Answer Checked By - Gilberto Lyons (WPSolving Admin)