You are not logged in.

#1 2010-07-01 16:41:18

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

[SOLVED] Regular expression warning with gcc

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

#2 2010-07-01 17:50:19

jdarnold
Member
From: Medford MA USA
Registered: 2009-12-15
Posts: 485
Website

Re: [SOLVED] Regular expression warning with gcc

Wouldn't

boost::regex(string(".*.").append(FileSuffix))

work just as well?

Offline

#3 2010-07-01 18:04:00

b52
Member
From: Germany
Registered: 2009-03-20
Posts: 49
Website

Re: [SOLVED] Regular expression warning with gcc

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

#4 2010-07-02 04:14:12

pseup
Member
Registered: 2008-06-06
Posts: 103

Re: [SOLVED] Regular expression warning with gcc

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

#5 2010-07-02 08:04:38

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Re: [SOLVED] Regular expression warning with gcc

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

#6 2010-07-02 15:50:14

b52
Member
From: Germany
Registered: 2009-03-20
Posts: 49
Website

Re: [SOLVED] Regular expression warning with gcc

What about a WORKING piece of code?
The regex is correct, so somethin is wrong with your input ...


srsly?

Offline

#7 2010-07-02 16:28:46

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [SOLVED] Regular expression warning with gcc

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

#8 2010-07-02 17:49:02

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 876

Re: [SOLVED] Regular expression warning with gcc

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! smile

Last edited by snack (2010-07-02 17:49:15)

Offline

Board footer

Powered by FluxBB