2

I am currently trying to write a class function that will replace every occurence of a word in a string with an other word , using C++11 lib.

void replace(std::string subject, std::string pattern, std::string replacement)
{
    std::regex exp(pattern);
    std::cout <<  std::regex_replace(subject, exp, replacement); 
}


replace(std::string("Hello world"),std::string("world"),std::string("planet")); 

returns nothing.

I guess that the problem is regular expression , but i have no idea and cannot find anywhere how to make a regexp matching an certain string using ECMAScript or any other availible in .

Does anyone know how to solve this problem ?

pawels1991
  • 91
  • 1
  • 6

1 Answers1

1

For completeness: As Xaqq and Captain Obvlious stated in the comments, regex is not yet supported by gcc.

See this gcc libstdc++ Status Page for reference.

On MSVC 2012 your functions works with the desired output.

replace("Hello world", "^(.*)world(.*)$", "$1planet$2");

Hello planet

Pixelchemist
  • 24,090
  • 7
  • 47
  • 71