バックスラッシュが前にない限り、文字列内の文字のすべての出現を置き換える素敵でエレガントな方法 (boost::algorithm::replace を使用) はありますか?
となることによって
std::string s1("hello 'world'");
my_replace(s1, "'", "''"); // s1 becomes "hello ''world''"
std::string s2("hello \\'world'"); // note: only a single backslash in the string
my_replace(s2, "'", "''"); // s2 becomes "hello \\'world''"
boost::regex を使用すると、次を使用してこれを行うことができます。
std::string my_replace (std::string s, std::string search, std::string format) {
boost::regex e("([^\\\\])" + search);
return boost::regex_replace(s, e, "\\1" + format);
}
しかし、パフォーマンス上の理由から、boost::regex を使用しないことを好みます。boost::algorithm::replace はぴったりのように見えますが、正確な方法がわかりません。