Some trouble with "regex_replace()"
Hi all, Using "regex_replace(string, pattern, replace_string, flags)" I got some unwished results. I can't find out what flag set I have to use, that the replacement string was taken, as it is. I think this short example should make my problem clear: Replace in a string all occurences of PATH through the actual windows directory: "Actual path: 'PATH'" with Path "D:\temp\rest" leads to the string: "Actual Path: 'D: emp est'" This show, that "\t" and "\r" was interpreted. For me that is an unexspected behaviour, because the replacement is just some string, not more. I understand that the pattern will be interpreted and controlled over the flags, but why the replacement string? Can somebody explain that behaviour and show me a way out? regards Arno
Ben Hutchings
Arno Schäfer wrote:
Hi all,
Using "regex_replace(string, pattern, replace_string, flags)" I got some unwished results. I can't find out what flag set I have to use, that the replacement string was taken, as it is. <snip>
Post your code.
Ben.
Here is my test case: <snip> #include <iostream> #define BOOST_NO_WREGEX #include "boost/regex.hpp" using namespace std; using namespace boost; int main(int argc, char *argv[], char *envp[]) { string source_org("My path: '#PATH#'"); regex pattern("#PATH#"); string replace("D:\\temp\\rest"); source_org += " " + source_org; bool only_first = false; while ( true ) { string source(source_org); regex_constants::match_flag_type flags = regex_constants::match_default; if ( only_first ) flags |= regex_constants::format_first_only; string result = regex_replace(source, pattern, replace, flags); cout << "Pattern \"" << pattern.expression() << "\" in string \"" << source << "\" should be replaced with \"" << replace << "\"" << endl << "\"" << result << "\"" << endl << endl; if ( only_first ) break; only_first = !only_first; } return 0; } Ouput: Pattern "#PATH#" in string "My path: '#PATH#' My path: '#PATH#'" should be replaced with "D:\temp\rest" est'"My path: 'D: emp Pattern "#PATH#" in string "My path: '#PATH#' My path: '#PATH#'" should be replaced with "D:\temp\rest" est' My path: '#PATH#'" Exspected output: Pattern "#PATH#" in string "My path: '#PATH#' My path: '#PATH#'" should be replaced with "D:\temp\rest" "My path: 'D:\temp\rest' My path: 'D:\temp\rest'" Pattern "#PATH#" in string "My path: '#PATH#' My path: '#PATH#'" should be replaced with "D:\temp\rest" "My path: 'D:\temp\rest' My path: '#PATH#'" <snip> Arno
Using "regex_replace(string, pattern, replace_string, flags)" I got some unwished results. I can't find out what flag set I have to use, that the replacement string was taken, as it is. I think this short example should make my problem clear: Replace in a string all occurences of PATH through the actual windows directory: "Actual path: 'PATH'" with Path "D:\temp\rest" leads to the string: "Actual Path: 'D: emp est'" This show, that "\t" and "\r" was interpreted. For me that is an unexspected behaviour, because the replacement is just some string, not more. I understand that the pattern will be interpreted and controlled over the flags, but why the replacement string? Can somebody explain that behaviour and show me a way out?
You have to double up escapes, and also escape '$'s as well: see the format string syntax for a guide, maybe I should add a "format_literal" option as well though, John.
John Maddock
You have to double up escapes, and also escape '$'s as well: see the format string syntax for a guide, maybe I should add a "format_literal" option as well though,
I am wrapping some classes in our existing application, which have the interface that a regular expression will be replaced exactly with the given string, so I have no influence to the input and I don't know what things are in this string, what was given to me. So my hope was, that I can force this behaviour with some flags, but what I see from your answer it will not exist. So can you give me a hint where the best location is, where I can implement "format_literal" for my personal boost library? I need this functionality! Arno
I am wrapping some classes in our existing application, which have the interface that a regular expression will be replaced exactly with the given string, so I have no influence to the input and I don't know what things are in this string, what was given to me. So my hope was, that I can force this behaviour with some flags, but what I see from your answer it will not exist. So can you give me a hint where the best location is, where I can implement "format_literal" for my personal boost library? I need this functionality!
Why not use regex_iterator to enumerate the matches, and replace them with your string? John.
John Maddock
Why not use regex_iterator to enumerate the matches, and replace them with your string?
John.
I have to use this replacement in my own string class which is inherited from std::string. I try it and it seems, that I have some trouble with the understanding of regex_iterator, also the existing examples doesn't help me :-( Is it possible for you, to look at my example and give me some hint, how to implement this case, with the regex_iterator? Many thanks Arno
I have to use this replacement in my own string class which is inherited from std::string. I try it and it seems, that I have some trouble with the understanding of regex_iterator, also the existing examples doesn't help me :-( Is it possible for you, to look at my example and give me some hint, how to implement this case, with the regex_iterator?
std::string replace_with_literal(const std::string& text, const boost::regex& re, const std::string& replace) { std::string result; sregex_iterator it(text.begin(), text.end(), re); sregex_iterator end; std::string::const_iterator last_end = text.begin(); while(it != end) { result.append(it->prefix().str()); result.append(replace); last_end = it->suffix().first; } result.append(last_end, text.end()); return result; } John.
Hi John, many thanks for the example. This make me the iterator stuff pretty clear now. After adding the "it++" in the while loop it works fine. What's about your thought about the "format_literal" flag? You think it will be implemented in future? regards Arno
many thanks for the example. This make me the iterator stuff pretty clear now. After adding the "it++" in the while loop it works fine. What's about your thought about the "format_literal" flag? You think it will be implemented in future?
Yes absolutely, however I'm trying to finish off the next revision of the library right now, so any new features will be patched against that code base, and not the current version (otherwise I end up doing everything twice, and get nothing done :-) ). John.
participants (3)
-
Arno Schäfer
-
Ben Hutchings
-
John Maddock