Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile and regex in c++ using eclipse [duplicate]

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 ?

like image 729
David Hamelech Avatar asked Oct 04 '22 01:10

David Hamelech


1 Answers

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

like image 192
Al Prihodko Avatar answered Oct 13 '22 12:10

Al Prihodko