开发者

Print .pdf filenames from a directory with Boost.regex

开发者 https://www.devze.com 2023-03-01 08:06 出处:网络
This is my code: path Path = \"e:\\\\Documents\\\\\"; boost::regex reg(\"(*.pdf)\"); for(recursive_directory_iterator it(Path); it != recursive_directory_iterator(); ++it)

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::reg开发者_StackOverflow中文版ex reg("(*.pdf)");

Am I not declaring the regex object good ?


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

精彩评论

暂无评论...
验证码 换一张
取 消