Print .pdf filenames from a directory with Boost.r

2019-05-04 00:22发布

问题:

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 ?

回答1:

*.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)


标签: c++ boost