从Boost.regex的目录打印.pdf文件名

问题描述:

这是我的代码:

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 Studio,运行程序后,问题是在这一行:

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

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

=

*。pdf 不是正则表达式,您需要

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

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




  • :匹配任何一个字符

  • * :0或以上的匹配
  • code> \\ :使一个 \ 用于转义(忽略下一个字符的正则表达式意义) li>

    • .: 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)