采用正则表达式获取某路径下符合特定模式字符串的文件名

前端之家收集整理的这篇文章主要介绍了采用正则表达式获取某路径下符合特定模式字符串的文件名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
(昨天晚上写了一点代码,利用中午吃饭的一点时间,整理一下,供大家参考吧。编码能力,亦如逆水行舟,不进则退。不管怎样,每天最少50行代码,一定要坚持啊。)

关于文件操作的功能定义公共的头文件

#ifndef COMMON_INCLUDE_H
#define COMMON_INCLUDE_H

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
//#include <sstream>
#include <boost/regex.hpp>

using namespace std;

#endif


定义文件名模式匹配功能的FindExprStrInFile类头文件
#ifndef FIND_FILE_NAME_ACCORD_EXPR_H
#define FIND_FILE_NAME_ACCORD_EXPR_H

#include <windows.h>
#include "CommonInclude.h"

class FindFileNameAccordExpr
{
private:
//bool _isFullPath;
string _dir;
string _searchString;
int _count;
vector<string> _vecfindStrs;

public:
FindFileNameAccordExpr(string dir,string expr);
bool find();
int getCount();
vector<string>::const_iterator getFirstIter();
vector<string>::const_iterator getLastIter();
};

#endif


定义文件名模式匹配功能的FindExprStrInFile类的定义文件


#include "FindFileNameAccordExpr.h"

FindFileNameAccordExpr::FindFileNameAccordExpr(string dir,string exprStr)
{
_dir = dir;
_searchString = exprStr;
_count = 0;
}

bool FindFileNameAccordExpr::find()
{
if ( _dir == "" || _searchString == "")
{
cout<< "input parameter is null!" << endl;
return false;
}

boost::regex expression(_searchString);
boost::smatch what;

WIN32_FIND_DATA FindFileData;
HANDLE hFind;
int find = 1;
string filePath = _dir + "/" + "*.*";

//find all files in current dir,and save in the vector fileNames.
hFind = FindFirstFile(filePath.c_str(),&FindFileData);
string curFileName;
vector<string> fileNames;
if (hFind == INVALID_HANDLE_VALUE)
{
cout<< "Invalid File Handle. GetLastError reports:"<<GetLastError()<<endl;
return false;
}
else
{
fileNames.push_back(FindFileData.cFileName);
while(FindNextFile(hFind,&FindFileData)!=0)
{
if((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)==0)
{
cout<<"File Name is: "<<FindFileData.cFileName<<endl;
curFileName = FindFileData.cFileName;
fileNames.push_back(curFileName);
}
}
FindClose(hFind);
}
if(fileNames.empty())
{
cout<<"Warning>>> The Dir is empty."<<endl;
return false;
}
else
{
vector<string>::const_iterator pos;
for(pos=fileNames.begin(); pos<fileNames.end(); ++pos)
{
if (boost::regex_search(*pos,what,expression))
{
cout <<"Current file name is match the expression,file name is: "<< what[0] <<endl;
_vecfindStrs.push_back(what[0]);
}
}
if(_vecfindStrs.empty())
{
return false;
}
else
{
return true;
}
}

return true;
}

int FindFileNameAccordExpr::getCount()
{
return _count;
}

vector<string>::const_iterator FindFileNameAccordExpr::getFirstIter()
{

return _vecfindStrs.begin();

}

vector<string>::const_iterator FindFileNameAccordExpr::getLastIter()
{
return _vecfindStrs.end();
}


定义main函数验证该类的实现是否正确。


#include "FindFileNameAccordExpr.h"

int main()
{

//FindFileNameAccordExpr fileExpr("D:","*.txt");
FindFileNameAccordExpr fileExpr("D:",".*\\.txt");
if(fileExpr.find())
{
cout<<"find the files"<<endl;
}

getchar();

}


注释:
我使用的是VS2008 实现的,默认建立的空工程是支持Unicode的,因此第一次编译不会通过。

错误原因:WinBase.h中定义如下,
#ifdef UNICODE
#define FindFirstFile FindFirstFileW
#else
#define FindFirstFile FindFirstFileA
#endif // !UNICODE

我们这里使用的都是ASCII,因此使用的FindFirstFileA函数

修改这一工程属性方法




The MIT license:

Permission is hereby granted,freeof charge,to any person obtaining a copy of this software and associated documentation files (the "Software"),to deal in the Software without restriction,including without limitation the rights to use,copy,modify,merge,publish,distribute,sublicense,and/or sell
copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS",WITHOUT WARRANTY OF ANY KIND,EXPRESS ORIMPLIED,INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE,ARISING FROM,OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
原文链接:https://www.f2er.com/regex/362841.html

猜你在找的正则表达式相关文章