0

I'm trying to learn how to use the regex library in c++11. On ubuntu 13.10 I'm trying to compile the following example from cplusplus.com:

// regex_replace example
#include <iostream>
#include <string>
#include <regex>
#include <iterator>

int main ()
{
  std::string s ("there is a subsequence in the string\n");
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  // using string/c-string (3) version:
  std::cout << std::regex_replace (s,e,"sub-$2");

  // using range/c-string (6) version:
  std::string result;
  std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");
  std::cout << result;

  // with flags:
  std::cout << std::regex_replace (s,e,"$1 and $2",std::regex_constants::format_no_copy);
  std::cout << std::endl;

  return 0;
}

With the command:

$ g++ -std=c++11 -o file file.cc

I get the following output:

file.cc:13:48: error: no matching function for call to ‘regex_replace(std::string&, std::regex&, const char [7])’

What am I doing wrong?! There's not much hair left on my head... Thanks in advance

Niklas
  • 3
  • 5

1 Answers1

4

You need to update your compiler from GCC 4.8 to GCC 4.9.

Unfortunately, GCC 4.9 has not been released yet, but it's stable enough to go with a trunk build if you need this functionality imminently.

Alternatively, give Clang or Boost.Regex a go.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055