help replace_all_regex and format_literal

Hi there,
I found that the behavior of replace_all_regex is weird while specifing format flag as format_literal.
For example, I would like to replace %f and %g in a source with some
text respectively. The code is
string source = "#f = %f \r\n #g = %g \r\n";
string text1("

Hi, llwaeva@21cn.com wrote:
Hi there, I found that the behavior of replace_all_regex is weird while specifing format flag as format_literal. For example, I would like to replace %f and %g in a source with some text respectively. The code is
string source = "#f = %f \r\n #g = %g \r\n"; string text1("
"); string text2("""name"" "); replace_regex(source, boost::regex("%f"), text1, boost::regex_constants::format_literal); replace_regex(source, boost::regex("%g"), text2, boost::regex_constants::format_literal);
The result is fine. However, I would like to use replace_all_regex instead. i.e.
replace_all_regex( source, boost::regex("(%f|%g)"), string("(?1 loofon@56.com)(?2 ""name""
"), boost::regex_constants::format_literal); However, because of format_literal, what I get is
#f = (?1 loofon@56.com)(?2 "name"
") #g = (?1 loofon@56.com)(?2 "name" ") rather than #f = loofon@56.com #g = "name"
" In my case, I need to handle a heap of patterns and using replace_all_regex is convenient. Any way to use both replace_all_regex and format_literal without going into the trouble mentioned above?
The only solution that I can comeup with is to run replace_all_regex twice. Once for #f pattern and the other time for the #g pattern. If you use format_literal, it is obvious, that (?1 and (?2 format specifiers are not evaluated. Maybe there is another solution for this, but I don't know about it. Best regards, Pavol.

Pavol Droba wrote:
The only solution that I can comeup with is to run replace_all_regex twice. Once for #f pattern and the other time for the #g pattern.
If you use format_literal, it is obvious, that (?1 and (?2 format specifiers are not evaluated.
Maybe there is another solution for this, but I don't know about it.
Exactly, either the string contains format specifiers or it doesn't :-) You would have to: In each replacement string, escape all occurances of "[()$\\]" with \\$0, then join the modified replacement strings together using (?1) etc to create the format string. John.
participants (3)
-
John Maddock
-
llwaeva@21cn.com
-
Pavol Droba