llwaeva@21cn.com wrote:
hi there, I am working with a pattern-replace job with boost:regex. For convenience, I use replace_all_regex to do that. Here is my code
replace_all_regex( TEXT_TO_BE_REPLACED, regex_exp, format_string, format_all );
TEXT_TO_BE_REPLACED is a text containing some patterns like %a, %b and %c which will be repalced with a specific string(format_string) that is inputed by user. However, the input string (format_string) may have some characters related to the regular expression, such characters must be escaped before the substitution. The following code is for that purpose
replace_all( input, "$", "$$" ); replace_all( input, "\\", "\\\\" ); replace_all( input, ":", "\\:" ); replace_all( input, "?", "\\?" ); replace_all( input, "(", "\\(" ); replace_all( input, ")", "\\)" );
The substitution works fine sometimes and fails when the input string (format_string) contains character which is not escaped. I just wonder if there is any function defined in boost::regex to escape all SPECIAL characters.
Use "format_literal" rather than "format_all" and all the characters in the format string will be output "as is", and none will be treated as special characters. John.