Question

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 ?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top