Replace substring using regex with condition
题
I need to replace every substring in a string that matches a regex, something like this:
std::string result = boost::regex_replace("my# random *#string ##"
, boost::regex("#([^#*]*[*][^#*]*)#")
, tr);
std::string tr(const boost::smatch &m)
{
std::string replaceString;
bool ret = CheckIfWeNeedToReplace(m[0], replaceString);
if (ret)
return replaceString; // replacing
else
return m[0]; // remain the same
}
e.g. for string "my# random *#string ##"
there is one match for regex "#([^#*]*[*][^#*]*)#"
, that is " random *"
, and if CheckIfWeNeedToReplace
return false, then the string remain the same, if not - it will be something like this "my#NEW SUBSTRING#string ##"
My code doesn't work, what did I do wrong ?
没有正确的解决方案
不隶属于 StackOverflow