
Hi there, boost:regex is powerful in string replacing. I am working with a complicate case which need a faster method in replacing substring. Here is my problem. I have a big string contains some pattern %K{xxx}, where xxx is a filename which may vary from place to place. e.g. BIG_STRING = ... ... %K{abc.txt} ... ... ... ... %K{foo.ini} ... ... OK, what complicated is that: I must retrieve the filename from %K{xxx}, load the content of the file and then replace %K{xxx} with the content. I search the code with a loop string::const_iterator start, end; string filename, content; boost::regex re("%K\\{(.*)\\}"); boost::match_resultsstd::string::const_iterator what; boost::match_flag_type flags = boost::match_default; start = BIG_STRING.begin(); end = BIG_STRING.end(); while ( regex_search(start, end, what, re, flags) ) { if (what[1].first!=what[1].second) { filename = string(what[1].first, what[1].second); } content = LoadFile(filename); // transform the const_iterator to iterator string::iterator sbegin(BIG_STRING.begin()), send(BIG_STRING.begin()); std::advance(sbegin, distancestring::const_iterator(sbegin, what[0].first)); std::advance(send, distancestring::const_iterator(send, what[0].second)); // replace the content BIG_STRING.replace( sbegin, send, content ); // reset the current position of the source string to search the next pattern start = what[1].second; // I copy the code from the document of boost, don't know what the following code for flags |= boost::match_prev_avail; flags |= boost::match_not_bob; } The second approach is to retrieve the filename and load the content seperately, build the substitute format and replace all the patterns with replace_all_regex, e.g. string& escape_format( string& input ) { replace_all( input, "$", "$$" ); replace_all( input, "\\", "\\\\" ); replace_all( input, ":", "\\:" ); replace_all( input, "?", "\\?" ); replace_all( input, "(", "\\(" ); replace_all( input, ")", "\\)" ); return input; } stringstream reformat; string regex_text; int n=0; while ( regex_search(start, end, what, re, flags) ) { if (what[1].first!=what[1].second) { filename = string(what[1].first, what[1].second); } content = LoadFile(filename); regex_text += string(what[0].first, what[0].second); reformat << "(?" << ++n << " " << escape_format( content ) << ")"; // reset the current position of the source string to search the next pattern start = what[1].second; // I copy the code from the document of boost, don't know what the following code for flags |= boost::match_prev_avail; flags |= boost::match_not_bob; } // replace the whole string at last replace_all_regex( BIG_STRING, boost::regex(regex_text), reformat.str(), format_all ); But this approach cause exception!!! Any better idea for my problem? Thanks in advance.