您的位置:首页 > 编程语言 > C语言/C++

采用正则表达式获取文本文件中的特定模式字符串

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++