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

时间:2011-04-29 12:14:16

标签: c++ boost

这是我的代码:

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

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

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

我没有声明正则表达式对象好吗?

1 个答案:

答案 0 :(得分:4)

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

boost::regex reg("(.*\\.pdf)"); 
  • .:匹配任何一个字符
  • *:前一场比赛的0或更多
  • \\:单个\进行转义(忽略下一个字符的正则表达式意义)