文字列置換を実装するための C++ 関数を作成しています。関数は次のようになります。
using namespace std;
string Replace(const string & str, const string & strOld,
const string & strNew, bool useRegex, bool caseSensitive)
{
regex::flag_type flag=regex::basic;
if(!caseSensitive)
flag |= regex::icase;
if(!useRegex)
// WHAT TO DO ?
std::regex rx(strOld,flag);
string output=regex_replace(str,rx,strNew);
return output;
}
strOldinのすべての出現箇所を に置き換えstrますstrNew。私はそれを使用std::regexしstd::regex_replaceて実装しようとしました。useRegexが true の場合にうまく機能します。ただし、useRegexisの場合、それが正規表現文字列ではなく単なる文字列であるfalseことを伝えることができません。strOld
たとえば、次のように呼び出します。
string output=Replace("hello.",".","?",false,true);
"??????"と思っているうちに戻ってきます"hello?"。