You are not logged in.
Hi, in my code I search for files with a certain extension using boost::regex in this way:
boost::regex(string(".*\.").append(FileSuffix))
This actually works and my code find any file whose extension matches the one specified in the variable FileSuffix. The annoying thing is that gcc gives me this warning at compile time:
warning: unknown escape sequence '\.'
As far as I know, this is because "\." is not a recognized C string escape sequence, so what the compiler does is to give the warning and then print "\." in the string literally. So I tried to substitute it with "\\.", which gives no warning but makes my program find no files.
Is there a way to specify regular expressions escapes as C strings without triggering the compiler warning?
Last edited by snack (2010-07-02 17:49:38)
Offline
Wouldn't
boost::regex(string(".*.").append(FileSuffix))
work just as well?
Offline
A \ is in C/C++ an escape char, so if you want to use \ as a regular char you have to escape it like this:
boost::regex(string(".*\\.").append(FileSuffix))
srsly?
Offline
You could use "[.]" for a literal "." and avoid the warning.
boost::regex(string(".*[.]").append(FileSuffix))
As to what you might be 'missing' to have gcc not complain in the first place, I have no idea.
Offline
Thanks guys for the replies!
@jdarnold: that would work, but it would match any character before the extension. For example, if the extension is txt you will find my.txt but also mytxt, yourtxt and every file name that ends with txt, being it an extension or not.
@b52: as I said in my origina post I tried and effectively this eliminates the warning,but then no file is found...
@pseup: same as the solution proposed by b52...
Well, since they all seem correct solutions I think it is a problem with my program. I search for files in this way:
namespace fs = boost::filesystem;
fs::path basePath("/path/to/files/");
boost::regex pattern(string(".*\.").append(FileExtension));
vector<fs::path> foundFiles;
fs::directory_iterator endItr; // Default constructor yields past-the-end
for (fs::directory_iterator i(basePath); i != endItr; ++i) {
// Skip if not a file
if (!is_regular_file(i->status()))
continue;
boost::smatch what;
// Skip if no match
if (!boost::regex_match(i->leaf(), what, pattern))
continue;
// File matches, store it
foundFiles.push_back(i->leaf());
}
I found this chunk of code on the web and reused it for my purposes, so I don't have complete control over it. Maybe the apparently good solutions proosed by you guys don't work because of this code...
Offline
What about a WORKING piece of code?
The regex is correct, so somethin is wrong with your input ...
srsly?
Offline
1. path::leaf() is deprecated, use i->filename() instead.
2. It is hard to debug this with out a complete, compilable code example, but here's my guess: FileExtension already contains the dot (i.e., FileExtension == ".txt" or something). If thats true, then you're trying to match the regex ".*[.].txt", which will match foo.?txt, but not foo.txt. Instead, you'll want string(".*\\").append(FileExtension).
Offline
tavianator guessed correctly: the extension already contained the "." and I didn't check the config file where it was defined. My fault, thank you all for the help!
Last edited by snack (2010-07-02 17:49:15)
Offline