#include <iostream>
#include <regex>
#include <string>
using namespace std;

std::string replaceWord(std::string &str, std::string& search, std::string& replace)
{
	// Escape the literal regex pattern
    search = std::regex_replace(search, std::regex(R"([.^$|{}()[\]*+?/\\])"), std::string(R"(\$&)"));
    // Escape the literal replacement pattern
    replace = std::regex_replace(replace, std::regex("[$]"), std::string("$$$$"));
    std::regex e("(\\W|^)("+search+")(?!\\w)");
    return std::regex_replace(str, e, std::string("$1") + replace);
}

int main() {
	std::string text("String toReplace()");
	std::string s("toReplace()");
	std::string r("theReplacement()");
	std::cout << replaceWord(text, s, r);	
	return 0;
}