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

One thought on “C++ Replace All Matches

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s