使用Boost.regex从目录中打印.pdf文件名

Print .pdf filenames from a directory with Boost.regex

本文关键字:打印 pdf 文件名 Boost regex 使用      更新时间:2023-10-16

这是我的代码:

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;
    }
}

但是我总是得到和Abort()错误在Visual Studio中,运行程序后,问题在这一行:

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

*.pdf不是正则表达式,它是一个glob(用于文件匹配)。你需要

boost::regex reg("(.*\.pdf)"); 
  • .:匹配任意一个字符
  • *: 0个或以上的前一个匹配
  • \:生成单个用于转义(忽略下一个字符的正则表达式含义)