On Sat, Dec 13, 2003 at 12:12:46PM -0000, John Maddock wrote:
I want to use regex to modify a string in place. In particular, I've read the contents of a file into a string, now I want to do some search/replace functions on the string, then write the modified string back out to the original file. I've looked through the regex documentation, including the October 2001 DDJ article, but I don't see any way to modify a string in place. From what I can tell, regex_merge looks like it does the kind of thing I want, except it doesn't seem to modify in place. I'd prefer to avoid creating two strings of each file's contents, one before regex processsing, one after. Is there functionality in regex that will let me modify a string in place?
Not directly, but you could:
repeated search through the string finding each match, then for each match found replace the matched section with the result of regex_format, and restart the search from the appropriate place.
John.
Alternatively, you can try to get string_algo library from the boost-sandbox. There is an algorithm replace_regex, that does in-place regex replacement. Pavol