I have a huge paragraph of text stored in an std::string named 'text'. On this string, I am replacing certain patterns with a white space using the boost regex library. Here is my code.
// Remove times of the form (00:33) and (1:33)
boost::regex rgx("\\([0-9.:]*\\)");
text = boost::regex_replace(text, rgx, " ");
// Remove single word HTML tags
rgx.set_expression("<[a-zA-Z/]*>");
text = boost::regex_replace(text, rgx, " ");
// Remove comments like [pause], [laugh]
rgx.set_expression("\\[[a-zA-Z]* *[a-zA-Z]*\\]");
text = boost::regex_replace(text, rgx, " ");
// Remove comments of the form <...>
rgx.set_expression("<.+?>");
text = boost::regex_replace(text, rgx, " ");
// Remove comments of the form {...}
rgx.set_expression("\\{.+?\\}");
text = boost::regex_replace(text, rgx, " ");
// Remove comments of the form [...]
rgx.set_expression("\\[.+?\\]");
text = boost::regex_replace(text, rgx, " ");
From my understanding, each time I run the regex_replace function, it creates an new string and writes the output to it. If I run the regex_replace function with N different patterns, it will allocate N new strings (deleting the old ones).
Since memory allocation is time consuming, is there a way to perform the replacement 'in-place', without allocating a new string?