4

This is my code:

path Path = "e:\\Documents\\";
boost::regex reg("(*.pdf)");
for(recursive_directory_iterator it(Path); it != recursive_directory_iterator(); ++it)
{
    if(boost::regex_search(it->string(), reg))
    {
        cout << *it << endl;
    }
}

But I always get and Abort() error in Visual Studio, after running the program, the problem is in this line:

boost::regex reg("(*.pdf)");

Am I not declaring the regex object good ?

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
Adrian
  • 19,440
  • 34
  • 112
  • 219

1 Answers1

4

*.pdf isn't a regex, it's a glob (for file matching). You need

boost::regex reg("(.*\\.pdf)"); 
  • .: matches any one character
  • *: 0 or more of the previous match
  • \\: to make a single \ for escaping (ignore the regex meaning of the next character)
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • Thank you, I thought it will work just like in linux, now its working good – Adrian Apr 29 '11 at 12:22
  • It is a standard regex just like when a regex is needed in linux. In a terminal, at the prompt, you are supposed to give a glob, not a regex. For grep, the matching argument is a regex, so you type `grep ` – Lou Franco Apr 29 '11 at 12:26
  • I got confused why you use 2 of "\" there, but I see that is working with one also – Adrian Apr 29 '11 at 12:30
  • That's because `.` matches any character including an actual `.`. It will also match filename-pdf, filename+pdf, etc. If you only want to match filename.pdf, use `\\.` – Lou Franco Apr 29 '11 at 12:37