采用正则表达式获取文本文件中的特定模式字符串
2013-06-09 21:38
555 查看
一直以为用Python写一些简单的小程序很方便,用C++编写由于可用的库太少,支持跨平台的库更少,因此比较费时费力。事实确实如此,但是灵活学习和使用一门语言,最好的方法就是把身边的一些小程序实现出来。在这个过程中不仅可以熟悉语言特性,而且可以把一些好的设计模式运用其中。
今天,这里我写了一段采用正则表达式获取文本文件中的特定模式字符串的小程序,希望对大家有帮助。
头文件如下:
#ifndef FIND_EXPR_STR_IN_FILE_H
#define FIND_EXPR_STR_IN_FILE_H
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <boost/regex.hpp>
using namespace std;
class FindExprStrInFile
{
private:
string _fileNameWithPath;
string _notification;
int _count;
vector<string> _findStrs;
public:
FindExprStrInFile(string name, string exprStr);
bool find();
int getCount();
vector<string>::const_iterator getFirstIter();
vector<string>::const_iterator getLastIter();
};
#endif
FindExprStrInFile类函数定义文件和main函数定义如下:
#include "FindExprStrInFile.h"
FindExprStrInFile::FindExprStrInFile(string name, string exprStr)
{
_fileNameWithPath = name;
_notification = exprStr;
_count =0;
}
bool FindExprStrInFile::find()
{
if (_fileNameWithPath == "" || _notification == "")
{
cout << "input parameter is null!" << endl;
return false;
}
std::ifstream fin(_fileNameWithPath.c_str());
if(!fin.is_open())
{
cout<<"The file path is not exist, please check it again."<<endl;
return false;
}
int nCount = 0;
boost::regex reg(_notification);
boost::smatch what;
while(!fin.eof())
{
std::string inbuf;
getline(fin, inbuf,'\n');
try {
std::string::const_iterator start = inbuf.begin();
std::string::const_iterator end = inbuf.end();
while(boost::regex_search(start, end, what, reg))
{
start = what[0].second;
//std::cout <<"Find it in current line: "<< what[0].str().c_str()<<std::endl;
_findStrs.push_back(what[0].str());
//_findStrs.push_back(what[1].str());
_count++;
}
}
catch(const boost::bad_expression& e)
{
std::cout <<"That's not a valid regular expression! (Error: " << e.what() << ") Exiting...\n";
}
}
fin.close();
return true;
}
int FindExprStrInFile::getCount()
{
return _count;
}
vector<string>::const_iterator FindExprStrInFile::getFirstIter()
{
return _findStrs.begin();
}
vector<string>::const_iterator FindExprStrInFile::getLastIter()
{
return _findStrs.end();
}
int main()
{
FindExprStrInFile exprFile("D:/Test.txt", "www\\.(.*)\\.com");
if(!exprFile.find())
{
cout<<"There is no string matched!"<<endl;
getchar();
return -1;
}
cout<<exprFile.getCount()<<endl;;
vector<string>::const_iterator iter;
for(iter=exprFile.getFirstIter(); iter!=exprFile.getLastIter(); ++iter)
{
cout<<"Matched string: "<<iter->c_str()<<endl;
}
getchar();
}
D:/Test.txt文件中内容
/******************************
www.sina.com
www.163.com
www.google.com.cn
www.hao123.com
www.csdn.net
*******************************/
运行结果:
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 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 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 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
今天,这里我写了一段采用正则表达式获取文本文件中的特定模式字符串的小程序,希望对大家有帮助。
头文件如下:
#ifndef FIND_EXPR_STR_IN_FILE_H
#define FIND_EXPR_STR_IN_FILE_H
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <boost/regex.hpp>
using namespace std;
class FindExprStrInFile
{
private:
string _fileNameWithPath;
string _notification;
int _count;
vector<string> _findStrs;
public:
FindExprStrInFile(string name, string exprStr);
bool find();
int getCount();
vector<string>::const_iterator getFirstIter();
vector<string>::const_iterator getLastIter();
};
#endif
FindExprStrInFile类函数定义文件和main函数定义如下:
#include "FindExprStrInFile.h"
FindExprStrInFile::FindExprStrInFile(string name, string exprStr)
{
_fileNameWithPath = name;
_notification = exprStr;
_count =0;
}
bool FindExprStrInFile::find()
{
if (_fileNameWithPath == "" || _notification == "")
{
cout << "input parameter is null!" << endl;
return false;
}
std::ifstream fin(_fileNameWithPath.c_str());
if(!fin.is_open())
{
cout<<"The file path is not exist, please check it again."<<endl;
return false;
}
int nCount = 0;
boost::regex reg(_notification);
boost::smatch what;
while(!fin.eof())
{
std::string inbuf;
getline(fin, inbuf,'\n');
try {
std::string::const_iterator start = inbuf.begin();
std::string::const_iterator end = inbuf.end();
while(boost::regex_search(start, end, what, reg))
{
start = what[0].second;
//std::cout <<"Find it in current line: "<< what[0].str().c_str()<<std::endl;
_findStrs.push_back(what[0].str());
//_findStrs.push_back(what[1].str());
_count++;
}
}
catch(const boost::bad_expression& e)
{
std::cout <<"That's not a valid regular expression! (Error: " << e.what() << ") Exiting...\n";
}
}
fin.close();
return true;
}
int FindExprStrInFile::getCount()
{
return _count;
}
vector<string>::const_iterator FindExprStrInFile::getFirstIter()
{
return _findStrs.begin();
}
vector<string>::const_iterator FindExprStrInFile::getLastIter()
{
return _findStrs.end();
}
int main()
{
FindExprStrInFile exprFile("D:/Test.txt", "www\\.(.*)\\.com");
if(!exprFile.find())
{
cout<<"There is no string matched!"<<endl;
getchar();
return -1;
}
cout<<exprFile.getCount()<<endl;;
vector<string>::const_iterator iter;
for(iter=exprFile.getFirstIter(); iter!=exprFile.getLastIter(); ++iter)
{
cout<<"Matched string: "<<iter->c_str()<<endl;
}
getchar();
}
D:/Test.txt文件中内容
/******************************
www.sina.com
www.163.com
www.google.com.cn
www.hao123.com
www.csdn.net
*******************************/
运行结果:
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 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 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 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
相关文章推荐
- 采用正则表达式获取某路径下符合特定模式字符串的文件名
- JS正则表达式获取字符串中特定字符的方法
- 利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)
- 用正则表达式获取字符串里的URL和文本
- 如何使用 awk 和正则表达式过滤文本或文件中的字符串
- python读配置文件;正则表达式替换、切割字符串;获取当前时间
- 如何使用 awk 和正则表达式过滤文本或文件中的字符串
- JS正则表达式获取字符串中特定字符的方法
- awk 系列:如何使用 awk 和正则表达式过滤文本或文件中的字符串
- 使用正则表达式找出不包含特定字符串的条目
- js利用正则表达式筛选并获取字符串中的URL链接(只是筛选完整URL,单独域名不筛选)
- C# 实现文件文本的正则表达式查询
- jQuery获取上传文件的名称的正则表达式
- PHP 正则表达式,反向引用(获取匹配的字符串内容)
- 使用正则表达式找出不包含特定字符串的条目
- Python练习代码 -- 字符串和正则表达式, 文件文件夹操作
- java使用正则表达式获取字符串中的数字
- Java使用正则表达式获取子文本的方法示例
- 正则表达式截取两个特定字符串之间的字符串
- java 正则表达式获取两个字符中间的字符串方法