linux目录正则表达式,Linux使用正则表达式自动搜索目录下文件。

写了一个小函数,贴出来给参考参考。输入是,你要搜索的顶层目录,第二个参数是你需要存放结果的列表,第三个参数,为你写的正则表达式。十分简单。效率很高。

#include

#include

#include

using namespace std ;

void RegexMatchAndSave(const char *FileName,char *path,list *filelist,string regexexpression)

{

regex Hrdacexp(regexexpression);

cmatch what; string in=FileName ;

if(regex_match(in.c_str(),what,Hrdacexp))

{

char buf[500];memset(buf,0,500);

strcat(buf,path); strcat(buf,what[0].str().c_str());

filelist->push_back(buf);

}

else

{

cout<

}

}

void SearchFilesOnPath(char *path,list *filelist,string regexexpression)

{

DIR *dirptr = NULL;

struct dirent *entry;

if(path==NULL)

{

printf("please input dir you want to read\n");

Fprint_String("PATH NULL ERROR","StdDataServerError","a+");

exit(-1);

}

if((dirptr = opendir(path)) == NULL)

{

printf("open dir %s !\n",path);

}

else

{

do

{

entry = readdir(dirptr) ;

if(entry!=NULL)

{

if(entry->d_type == 0x04 && (strcmp((const char*)entry->d_name,"..")!=0) && (strcmp((const char*)entry->d_name,".")!=0))

{

printf("###%s is dir and isn't \" ..\" nor \".\"\n", entry->d_name);

char buf[500];memset(buf,0,500);

strcat(buf,path); strcat(buf,entry->d_name);strcat(buf,"/");

SearchFilesOnPath(buf,filelist,regexexpression);

}

else if (entry->d_type == 0x08)

{

printf("%s is file ,match now \n", entry->d_name);

RegexMatchAndSave(entry->d_name,path,filelist,regexexpression);

}

else{

printf("What type is it ??\n");

}

}

}while (entry);

closedir(dirptr);

printf("File Search over now to Show consequence\n\n");

list::iterator iter;

for(iter=filelist->begin();iter!=filelist->end();iter++)

{

printf("%s\n",(*iter).c_str());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值