Replace Substring of digits by Sum

  c++, regex, string, substring

I have a string that’s in the form similar to:

"1111P1P"

I’m trying to replace all sub strings of ones by the total:

i.e. "4P1P"

The string will always contain 1’s and no other numbers before replacement.

My initial idea was to split the string using regex and store it in a vector and i could manipulate it. But this removes the delimiters as well.

std::string newDes = "1111P1P";
std::vector<std::string> desSplit; 
std::regex re1("[^0-9]");
std::sregex_token_iterator first1{newDes.begin(), newDes.end(), re1, -1}, 
desSplit = {first1, last1};

Any help would really be appreciated.

Source: Windows Questions C++

LEAVE A COMMENT