You are not logged in.
Pages: 1
I'm writing a tiny routine to convert glob expressions into regular expressions. For that I need to escape each "." character in the glob string. Currently I'm doing like this:
regExpFiles.replace(pos, 1, "\\.");
where pos is the position of the "." character inside the string (previously found with string::find). Doing in this way I get two slashes in the regula expression string, i.e. "." is replaced by "\\.". If I use only one slash in my replacement expression:
regExpFiles.replace(pos, 1, "\.");
I get a warning about an unrecognized escape sequence, and "." remains ".". With three slashes, same warning and a resulting "\\.". With four slashes, no warning and a resulting "\\\\."
Maybe I'm a dummy but I tried every possible reasonable combination of slashes in the definition of the replacement string so I believe that at least one of them should work as I desire... is there a way to obtain a single slash (or more generally: an odd number of slashes)? By the way, string::append show the same behavior.
Thanks.
Offline
Best guess is that you're doing the replacement twice. Does the following work as expected?
#include <iostream>
#include <string>
int main()
{
std::string s = "This is a test.";
std::cout << s.length() << ": " << s << std::endl;
std::string::size_type pos = s.find('.');
s.replace(pos, 1, "\\.");
std::cout << s.length() << ": " << s << std::endl;
return 0;
}
This is weird. The example of aoba works as expected but debugging with gcc inside Eclipse shows the incorrect behavior that I reported on the first post. In other words, on standard output I get "This is a test\." while gdb shows "This is a test\\.". I previously didn't try to print my results but only checked with gdb, so that's why I thought there was an error. Anyway, is there a reason why gdb reports a different value for the string? Or is it just a bug?
Offline
std:count << "a string\\." << std::endl;
will print
a string\.
One is the contents of the string variable, the other is what is displayed on screen when that variable is printed.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Correct. I didn't think about it. Thanks for the clarification, Trilby.
Offline
Pages: 1