C++ 如何搜索具有特定模式的文件路径

C++ 如何搜索具有特定模式的文件路径,c++,boost,filesystems,C++,Boost,Filesystems,如果文件与模式匹配,我正在寻找一种在目录(及其子目录)中查找文件的方法 我有以下代码: inline static void ScanForFiles(std::vector<string> &files,const path &inputPath,string filter="*.*",bool recursive=false) { typedef vector<boost::filesystem::path> vec;

如果文件与模式匹配,我正在寻找一种在目录(及其子目录)中查找文件的方法

我有以下代码:

 inline static void ScanForFiles(std::vector<string> &files,const path &inputPath,string filter="*.*",bool recursive=false)
 {
        typedef vector<boost::filesystem::path> vec;             // store paths,
        vec v;                                // so we can sort them later

        copy(directory_iterator(inputPath), directory_iterator(), back_inserter(v));
        for(int i=0; i<v.size(); i++)
        {
            if(IsDirectory(v[i]))
            {
                if(recursive)
                {
                    ScanForDirs(files,v[i],recursive);
                }
            }
            else
            {
                if(File::IsFile(v[i]))
                {
                    files.push_back(v[i].string());
                }
            }
        }
}
inline static void ScanForFiles(std::vector&files,const path&inputPath,string filter=“*.*”,bool recursive=false)
{
typedef vector vec;//存储路径,
vec v;//这样我们以后可以对它们进行排序
复制(目录迭代器(inputPath)、目录迭代器()、返回插入器(v));

对于(inti=0;i,我想出了以下代码片段:

#include <iostream>
#include <string>
#include <boost/regex.hpp>

std::string escapeRegex(const std::string &str) {
  boost::regex esc("([\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\\\])");                                                         
  std::string rep("\\\\\\1");
  return regex_replace(str, esc, rep, boost::match_default | boost::format_sed);
}

std::string wildcardToRegex(const std::string &pattern) {
  boost::regex esc("\\\\([\\*\\?])");
  std::string rep(".\\1");
  return regex_replace(escapeRegex(pattern), esc, rep, boost::match_default | boost::format_sed);
}

using namespace std;
using namespace boost;
int main(int argc, char **argv) {
  string pattern = "test/of regexes/*.jpg";
  cout << wildcardToRegex(pattern) << endl;
}
#包括
#包括
#包括
std::string escapeRegex(const std::string&str){
boost::regex-esc(([\\^\.\$\\\\\\\\\\\\(\\)\\[\\]\*\\\+\?\\/\\\\\]);
字符串代表(“\1”);
返回regex_replace(str、esc、rep、boost::match_default | boost::format_sed);
}
std::string wildcardToRegex(const std::string和pattern){
boost::regex-esc(“\\\\([\\*\\?])”;
标准::字符串代表(“.\\1”);
返回regex_replace(escapeRegex(pattern)、esc、rep、boost::match_default | boost::format_sed);
}
使用名称空间std;
使用名称空间boost;
int main(int argc,字符**argv){
string pattern=“test/of regexes/*.jpg”;

这看起来像是@ales\u t的副本:问题是将过滤器从正常类型的“*.jpg”转换为正则表达式格式。类似这样的东西就是正则表达式过滤器(“*.jpg”)不起作用。我需要要求此函数的用户以不好的正则表达式格式编写筛选器,或者转换它,但我似乎无法。有解决方案吗?您可以尝试编写一个函数,将通配符转换为正则表达式。只要您只支持
*
,您就可以退出模式,然后替换
\*e> 使用
*
\?
使用
时,请参见此处:另一方面,例如bash通配符更丰富,编写一个处理其所有功能的转换函数可能会很棘手。@ales\u t:about(){}<>等等,我认为它们也应该被转换。
#include <iostream>
#include <string>
#include <boost/regex.hpp>

std::string escapeRegex(const std::string &str) {
  boost::regex esc("([\\^\\.\\$\\|\\(\\)\\[\\]\\*\\+\\?\\/\\\\])");                                                         
  std::string rep("\\\\\\1");
  return regex_replace(str, esc, rep, boost::match_default | boost::format_sed);
}

std::string wildcardToRegex(const std::string &pattern) {
  boost::regex esc("\\\\([\\*\\?])");
  std::string rep(".\\1");
  return regex_replace(escapeRegex(pattern), esc, rep, boost::match_default | boost::format_sed);
}

using namespace std;
using namespace boost;
int main(int argc, char **argv) {
  string pattern = "test/of regexes/*.jpg";
  cout << wildcardToRegex(pattern) << endl;
}