使用wstring替换regex_replace需要帮助

need assist using regex_replace with wstring

本文关键字:帮助 replace wstring 替换 regex 使用      更新时间:2023-10-16

我想使用regex_replace,就像这里链接的例子一样,使用using string/c-string(3)版本:http://www.cplusplus.com/reference/regex/regex_replace/

问题是,我对模板和regex类的理解不够好,无法在每个示例中使用regex_replace,但使用了一个宽字符串。我看到了花环,但我不知道如何使用它。

其想法是获取字符串regex_{13E008E3-EEE7-4AC3-B9F1-E353DB67EDFD}并将其转换为状态_{13E808E3-EEE7-4AC3-B6F1-E353DB267EDFD}

wstring statusDataName;
wstring key              = wstring(L"regex_");
wstring repl             = wstring(L"status_");
TCHAR dataName[MAX_STR]  = {0};
statusDataName = regex_replace(wstring(dataName), key,  repl);

错误C2784:'std::basic_string&lt_Elem,std::char_traits&lt_Elem>,std::分配器&lt_其他>>std::regex_replace(const_Elem*,conststd::basic_regx&lt_Elem,RxTraits>&,常量勒姆(_E)*,std::regex_constants:match_flag_type)':无法从中推导出'const_Elem*'的模板参数'std::basic_string,std::allocater>'错误C2784:'std::basic_string&lt_Elem,std::char_traits&lt_Elem>,std::分配器&lt_其他>>std::regex_replace(const_Elem*,conststd::basic_regx&lt_Elem,RxTraits>&,conststd::basic_string&lt_Elem,_Traits1,_Alloc1>&,std::regex_constants::match_flag_type)':无法推导模板来自的"const_Elem*"的参数'std::basic_string,std::allocater>'错误C2784:"std::basic_string&lt_Elem,_Traits1,_Alloc1>std::regex_replace(const std::basic_string<_Elem,_Traits1,_Alloc1>&,const std::basic_regx&lt_Elem,RxTraits>&,常量勒姆(_E)*,std::regex_constants::match_flag_type)":无法推导"const std::basic_regex&lt_Elem,_RxTraits>&'从…起"std::wstring"错误C2784:"std::basic_string&lt_Elem,_Traits1,_Alloc1>std::regex_replace(const std::basic_string<_Elem,_Traits1,_Alloc1>&,const std::basic_regx&lt_Elem,RxTraits>&,conststd::basic_string&lt_Elem,_Traits2,_Alloc2>&,std::regex_constants::match_flag_type)':无法推导模板const std::basic_regx<的参数_Elem,_RxTraits>&'从…起"std::wstring"错误C2780:"_OutIt"std::regex_replace(_OutIt,_BidIt,_BidIt,conststd::basic_regx&lt_Elem,RxTraits>&,常量勒姆(_E)*,std::regex_constants::match_flag_type)':需要6个参数-提供了3个错误C2780:"_OutTy*std::regx_replace(_OutTy(&)[_OutSize],_BidIt,_BidIt,const std::basic_regx&lt_Elem,_RxTraits>&,const std::basic_string&lt_Elem,_Traits,_Alloc>&,std::regex_constants::match_flag_type)':需要6个参数-3提供的错误C2780:'_OutItstd::regex_replace(_OutIt,_BidIt,_BidIt,conststd::basic_regx&lt_Elem,RxTraits>&,conststd::basic_string&lt_Elem,_Traits,_Alloc>&,std::regex_constants::match_flag_type)':需要6个参数-3提供

我该怎么解决这个问题?

工作变体:

#include <string>
#include <regex>
#include <iostream>
using std::wstring;
int main()
{
    wstring dataName = L"regex_{13E008E3-EEE7-4AC3-B9F1-E353DB67EDFD} ";
    wstring key              = wstring(L"regex_");
    wstring repl             = wstring(L"status_");
    wstring statusDataName = std::regex_replace(dataName, std::wregex(key),  repl);
    std::wcout << statusDataName << L"n";
}