5

So my professor gave me a work with regex in c++.

So I tried to write my code in eclipse (I am using linux (ubuntu 12.04)).

so I took the code :

   // regex_search example
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("this subject has a submarine as a subsequence");
  std::smatch m;
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  std::cout << "Target sequence: " << s << std::endl;
  std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;
  std::cout << "The following matches and submatches were found:" << std::endl;

  while (std::regex_search (s,m,e)) {
    for (auto x:m) std::cout << x << " ";
    std::cout << std::endl;
    s = m.suffix().str();
  }

  return 0;
}

As you can see it is a simple code for working with regex.

so I try to build it and eclipse gives me an error:

Type 'smatch' could not be resolved

and also:

Type 'std::regex' could not be resolved

what is the problem ?

I tried to get add the flag -std=c++0x in the suitable location (properties->c/c++ build ->Miscellaneous)and nothing happen.

maybe I am doing it wrong ?

maybe I have to add a link to library like in pthread ?

3 Answers3

4

My setup:

Operation system: Ubuntu 14.04 Compiler: GCC 4.8.2 IDE: Eclipse 3.8.1

Problem:

I've included regex (#include <regex>) to my header file but my IDE keeps complaining that type of 'something' could not be resolved. I've tried using namespaces such as std:: and std::tr1, like people from google has adviced, with no success.

Solution:

Solution is as simple as the problem itself. If you take a look at usr/include/c++/4.8 you'll notice the regex file in root folder is kind of a stub. However there's another regex file in /tr1 folder.

The trick is, that instead of writing #include <regex>, you should write #include <tr1/regex>

After that you're able to use regex through std::tr1:: namespace.

For example like this: std::tr1::smatch

Hope that helps!

© http://antaumus.blogspot.com/2014/08/how-to-use-regex-with-gcc-482.html

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Ben Povar
  • 139
  • 4
0

regex is added under std::tr1 namespace. please declare the object for your regular expression as std::tr1::regex, it should work

DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • 1
    Thanks DevZer0, but i really don't understand what is the actual change you have made except font change? – Zeeshan Jul 11 '13 at 04:16
0

The flag is not enough, you have to add the preprocessor symbol " ____GXX_EXPERIMENTAL_CXX0X____" in "C/C++ General -> Paths and Symbols -> Symbols -> GNU C++" too. For details, please refer to the following question: Eclipse CDT C++11/C++0x support. It's also in the Eclipse wiki: http://wiki.eclipse.org/CDT/User/FAQ#CDT_does_not_recognize_C.2B.2B11_features. The second link also features an additional link to a forum page describing a "hidden" option (http://www.eclipse.org/forums/index.php/mv/msg/373462/909018/#msg_909018). If you configure the compiler with the "hidden" option as described in the link, it still works after removing the "____GXX_EXPERIMENTAL_CXX0X____" flag again. At least for me it worked this way (Eclipse Juno 4.2.2 and GCC 4.7.2).

Community
  • 1
  • 1
Marste
  • 627
  • 7
  • 22