Regex in C++: Requires compiler support error

16,979

Solution 1

and must be enabled with the -std=c++0x or -std=gnu++0x compiler options

Add one of those options, -std=c++0x or -std=gnu++0x, to your compiler command:

g++ -std=c++0x ...

Note if std::regex is not supported see boost::regex for an alternative.

Solution 2

Simply just add

g++ -std=gnu++0x <filename.cpp>

or

g++ -std=c++0x <filename.cpp>

It will work properly

Solution 3

It looks like you are trying to use the regex class, which is part of the new C++11 standard, but not telling the compiler to compile to that standard.

Add -std=c++0x to your compiler flags and try again.

EDIT : As the gcc implementation status page shows, the regex support in gcc is far from complete. So even adding the right flag wont help yet. If you need regex support, you could try boost.

Share:
16,979
Enigman
Author by

Enigman

Updated on June 04, 2022

Comments

  • Enigman
    Enigman almost 2 years

    I'm trying to use regex in C++. The following is my code:

    #include<iostream>
    #include<stdlib.h>
    #include<string.h>
    #include<regex>
    using namespace std;
    
    int main(int argc, char** argv) {
        string A = "Hello!";
        regex pattern = "(H)(.*)";
        if (regex_match(A, pattern)) {
            cout << "It worked!";
        }
        return 0;
    }
    

    But I'm encountering this error :

    In file included from /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/regex:35:0,
                     from main.cpp:12:
    /usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
    

    How can this be solved and what is wrong?

  • Enigman
    Enigman over 11 years
    How exactly do I do that? Thanks!
  • Enigman
    Enigman over 11 years
    How do I add it to my 'compiler flags?' I'm using NetBeans. Is there an option here that lets me do that?
  • Enigman
    Enigman over 11 years
    Oh. I'm not using the terminal to compile and run my code. I'm using netbeans.
  • hmjd
    hmjd over 11 years
    @Enigman, I don't use NetBeans. Google for how to add compiler options in NetBeans or search the NetBeans manual should provide you with a solution.
  • Enigman
    Enigman over 11 years
    Yeah. I shouldn't have asked that here! Sorry. Will do that.
  • Tom Macdonald
    Tom Macdonald over 11 years
    project properties -> Build -> C++ Compiler -> Additional Options I believe.
  • Enigman
    Enigman over 11 years
    I've done that. Still there seems to be error. :( I tried the code given at cplusplus.com/reference/regex/regex_match too.
  • Tom Macdonald
    Tom Macdonald over 11 years
    See my edit. Regex support in the gcc C++11 implementation is not complete.
  • Pete Becker
    Pete Becker over 11 years
    @Enigman - don't bother. GCC's implementation of regular expressions is unusable.