-1

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!

Archer Hu
  • 37
  • 11
  • 2
    See [Is gcc 4.8 or earlier buggy about regular expressions](http://stackoverflow.com/a/12665408/3832970) *How can I solve this problem?* > Upgrade to the latest GCC version. – Wiktor Stribiżew Mar 04 '16 at 10:15

1 Answers1

1

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

Community
  • 1
  • 1
Byoungchan Lee
  • 1,372
  • 13
  • 28
  • My CentOS7 is "gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) ", so it seems I have to upgrade my gcc and g++, but there is nothing happend after I type "yum upgrade gcc gcc-c++" – Archer Hu Mar 04 '16 at 12:11
  • And I confused why gcc shows 'std::regex_error' but regex not support in this version – Archer Hu Mar 04 '16 at 12:57