以下程序列出当前目录中名称与正则表达式匹配的文件filename_[0-5][34]:
#include <boost/filesystem.hpp>
#include <boost/regex.hpp> // also functional,iostream,iterator,string
namespace bfs = boost::filesystem;
struct match : public std::unary_function<bfs::directory_entry,bool> {
bool operator()(const bfs::directory_entry& d) const {
const std::string pat("filename_[0-5][34]");
std::string fn(d.filename());
return boost::regex_match(fn.begin(), fn.end(), boost::regex(pat));
}
};
int main(int argc, char* argv[])
{
transform_if(bfs::directory_iterator("."), bfs::directory_iterator(),
std::ostream_iterator<std::string>(std::cout, "\n"),
match(),
mem_fun_ref(&bfs::directory_entry::filename));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
transform_if()为简洁起见,我省略了定义.它不是标准功能,但应该直接实现.
| 归档时间: |
|
| 查看次数: |
1214 次 |
| 最近记录: |