Regular Expression 正则表达式-3 (C++)[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 dancefire 的 blog

最后用c++实现了一把,因为stl中尚未包含regular expression,因此我使用了boost中的regex++。不过因为不是很熟悉,所以代码很蹩脚,将就看了。呵呵。

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

using namespace std;

void readfile( const char* filename, string& str );
void writefile( const char* filename, const string str );
void filter( const string input, string& output );

int main(int argc, const char* argv[])
{
    if(
argc < 3 )
    {
        
cout<< "please enter 2 filenames(e.g. in.txt out.txt)" << endl;
        return
1;
    }
    
string strin, strout;

    
readfile( argv[1], strin );
    
filter( strin, strout );
    
writefile( argv[2], strout );
}

void readfile( const char* filename, string& str )
{
    
ifstream in( filename );
    
str.erase();
    
str.reserve( in.rdbuf()->in_avail() );
    
string strtemp;
    while( !
in.eof() )
    {
        
in >> strtemp;
        
str.append(strtemp);
    }
    
in.close();
}

本文关键:Regular Expression 正则表达式-3 (C++)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top