C++ Replace All Matches

#include <boost/algorithm/string/regex.hpp>

string str = “There are 30 apples, each of which is of different type!”;
string pattern = “([\\d]+)|([.,?!])|(of)”;
string replacement = “(?1##NUMBER##)(?2##SYMBOL##)(?3##OF##)”;

string newStr = boost::algorithm::replace_all_regex_copy(str, boost::regex(pattern), replacement, boost::regex_constants::format_all);

cout << newStr << endl;

————–

$ There are ##NUMBER## apples##SYMBOL## each ##OF## which is ##OF## different type##SYMBOL##

cf. replace_all_regex, erase_all_regex, erase_all_regex_copy

C++ Replace All Matches