std::regex_replace
certainly works for std::wstring
, and all other specializations of std::basic_string
. However, the replacement format string's character type must match the character type of the regex and input string. This means you need a wide replacement format string:
std::regex_replace(str, remove, L"");
Apart from that, the constructor of std::wregex
taking a const wchar_t*
is explicit, so your copy-initialization will not work. Also, this overload of std::regex_replace
returns a new string with the replacements done rather than doing them in place, which is something to be aware of.
Here is an example with these points considered:
std::wstring str = L"hello hi hello hi hello hi";
std::wregex remove{L"hi"};
str = std::regex_replace(str, remove, L"");