Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need assist using regex_replace with wstring

Tags:

c++

stl

I want to use regex_replace like the example linked here using using string/c-string (3) version: http://www.cplusplus.com/reference/regex/regex_replace/

The problem is I don't understand templates and the regex classes well enough to use regex_replace per example, but with a wide string. I saw the wregex, but I'm not seeing how I can use it.

The idea is to take a string regex_{13E008E3-EEE7-4AC3-B9F1-E353DB67EDFD} and turn it into status_{13E008E3-EEE7-4AC3-B9F1-E353DB67EDFD}

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

error C2784: 'std::basic_string<_Elem,std::char_traits<_Elem>,std::allocator<_Other>> std::regex_replace(const _Elem *,const std::basic_regex<_Elem,_RxTraits> &,const _Elem *,std::regex_constants::match_flag_type)' : could not deduce template argument for 'const _Elem *' from 'std::basic_string,std::allocator>' error C2784: 'std::basic_string<_Elem,std::char_traits<_Elem>,std::allocator<_Other>> std::regex_replace(const _Elem *,const std::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem,_Traits1,_Alloc1> &,std::regex_constants::match_flag_type)' : could not deduce template argument for 'const _Elem *' from 'std::basic_string,std::allocator>' error C2784: 'std::basic_string<_Elem,_Traits1,_Alloc1> std::regex_replace(const std::basic_string<_Elem,_Traits1,_Alloc1> &,const std::basic_regex<_Elem,_RxTraits> &,const _Elem *,std::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_regex<_Elem,_RxTraits> &' from 'std::wstring' error C2784: 'std::basic_string<_Elem,_Traits1,_Alloc1> std::regex_replace(const std::basic_string<_Elem,_Traits1,_Alloc1> &,const std::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem,_Traits2,_Alloc2> &,std::regex_constants::match_flag_type)' : could not deduce template argument for 'const std::basic_regex<_Elem,_RxTraits> &' from 'std::wstring' error C2780: '_OutIt std::regex_replace(_OutIt,_BidIt,_BidIt,const std::basic_regex<_Elem,_RxTraits> &,const _Elem *,std::regex_constants::match_flag_type)' : expects 6 arguments - 3 provided error C2780: '_OutTy *std::regex_replace(_OutTy (&)[_OutSize],_BidIt,_BidIt,const std::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem,_Traits,_Alloc> &,std::regex_constants::match_flag_type)' : expects 6 arguments - 3 provided error C2780: '_OutIt std::regex_replace(_OutIt,_BidIt,_BidIt,const std::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem,_Traits,_Alloc> &,std::regex_constants::match_flag_type)' : expects 6 arguments - 3 provided

How can I fix this?

like image 882
505HPC6Z06 Avatar asked Nov 09 '15 00:11

505HPC6Z06


1 Answers

Working variant:

#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";
}
like image 79
fghj Avatar answered Oct 07 '22 17:10

fghj