Is it possible to use std::tr1::regex_iterator to search and replace in a string ?
I know I can use std::tr1::regex_replace to replace all occurrences (or only one), but I need to replace each "pattern" that I find with something different, so I need to iterate my input string and replace on the fly.
std::string expression;
std::tr1::regex variableRegExpression;
std::tr1::sregex_iterator m1(
expression.begin(),
expression.end(),
variableRegExpression);
std::tr1::sregex_iterator m2;
std::vector<std::string> tokens;
for (; m1 != m2; ++m1)
{
std::string token = m1->str();
}
The above code will iterate all occurrences of what I'm searching for in the string
expression
.
What I'd like to do would be to replace each occurrence with something else.
I was able to do that with some loops with std::tr1::regex_search and std::regex_replace, but it does not look "clean" :
std::string expression;
std::tr1::regex variableRegExpression;
bool found = true;
while (found)
{
std::tr1::smatch matchResult;
found = std::tr1::regex_search(
expression, matchResult, variableRegExpression );
if ( found )
{
std::string newValue("Cloud1");
expression = std::tr1::regex_replace(
expression, variableRegExpression, newValue,
std::tr1::regex_constants::format_first_only);
}
else
{
found = false;
}
}
Question :
Is it possible to do a simple search and replace with the
regex_iterator
?
Thanks
Max.