Results 1 to 4 of 4

Thread: C++ Regex -- what am I missing?

  1. #1
    Join Date
    Nov 2006
    Location
    40.31996,-80.607213
    Beans
    Hidden!
    Distro
    Ubuntu

    Question C++ Regex -- what am I missing?

    I am trying to learn C++, and have a simple program here (from a tutorial, not mine) that I am trying to compile and run, but the compiler keeps complaining about
    "regex.cpp:1:17: error: regex: No such file or directory"

    Here is the program:
    Code:
    #include <regex>
    #include <iostream>
    #include <string>
    
    bool is_email_valid(const std::string& email)
    {
       // define a regular expression
       const std::tr1::regex pattern
          ("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
    
       // try to match the string with the regular expression
       return std::tr1::regex_match(email, pattern);
    }
    
    int main()
    {
       std::string email1 = "marius.bancila@domain.com";
       std::string email2 = "mariusbancila@domain.com";
       std::string email3 = "marius_b@domain.co.uk";
       std::string email4 = "marius@domain";
    
       std::cout << email1 << " : " << (is_email_valid(email1) ?
          "valid" : "invalid") << std::endl;
       std::cout << email2 << " : " << (is_email_valid(email2) ?
          "valid" : "invalid") << std::endl;
       std::cout << email3 << " : " << (is_email_valid(email3) ?
         "valid" : "invalid") << std::endl;
       std::cout << email4 << " : " << (is_email_valid(email4) ?
         "valid" : "invalid") << std::endl;
    
       return 0;
    }
    Initially, it is the first line that is causing all the problems. The compiler can't find <regex>. I even reinstalled boost, but I'm not sure that mattered much.

    Any ideas? (sorry, but I'm extremely new to this).
    "Security lies within the user of who runs the system. Think smart, live safe." - Dr Small
    Linux User #441960 | Wiki: DrSmall

  2. #2
    Join Date
    Oct 2006
    Location
    Tucson, AZ
    Beans
    1,420
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: C++ Regex -- what am I missing?

    The file in question ("/usr/include/c++/4.3/regex" on Intrepid) is part of the package "libstdc++6-4.3-dev". Try installing that (though it *should* have been installed as part of "build-essential", I believe).

    Also - could you post the compile command you're using?

    Lloyd B.
    Don't tell me to get a life.
    I had one once.
    It sucked.

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,265
    Distro
    Kubuntu

    Re: C++ Regex -- what am I missing?

    I believe the correct file to include is <tr1/regex>.

  4. #4
    Join Date
    Apr 2006
    Location
    Atlanta, USA
    Beans
    427

    Re: C++ Regex -- what am I missing?

    Including <regex> like you have is fine. You just need to pass "-std=c++0x" to g++ in order to enable the tr1 code.

    In file included from /usr/include/c++/4.3/regex:40,
    from main.cc:10:
    /usr/include/c++/4.3/c++0x_warning.h:36: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.
    Here we are, trapped in the amber of the moment. There is no why.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •