I am using boost xpressive regex_replace . After the replace I get garbage characters at the end of the string
std::wstring wEsc(L"fxSSyrpng");
std::wstring wReplaceByText(L"tiff");
std::wstring searchText(L"fx");
wsregex regExp;
try
{
regExp = wsregex::compile( searchText );
}
catch ( regex_error &/*error*/ )
{
throw ;
}
catch (...)
{
throw ;
}
std::wstring strOut;
strOut.reserve( wEsc.length() + wReplaceByText.length() );
std::wstring::iterator it = strOut.begin();
boost::xpressive::regex_replace( it, wEsc.begin() , wEsc.end(), regExp,
wReplaceByText, regex_constants::match_not_null );
After
reservestringstrOuthas still 0 elements. SostrOut.begin() == strOut.end()is true, anditpoints to nothing. If you want to use output iterator to write data inregex_replace,itmust point to memory with enough room to store all data. You can fix it by callingresizeinstead ofreserve.Another solution is to employ
back_inserterto do this job (operator=on this iterator push data intostring), thenitis unnecessary, and the code looks like: