最后用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(); } void writeFile( const char* filename, const string str ) { ofstream out( filename ); out.write( str.c_str(), (streamsize)str.length() ); out.flush(); out.close(); } void filter( const string input, string& output ) { output.erase(); output.reserve( input.length() ); boost::regex expression("\"(\\w+):(\\w+)%(\\w+)\""); boost::smatch group; boost::regex_constants::match_flag_type flags = boost::regex_constants::match_default; string::const_iterator start, end;  
[1] [2] 下一页
Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved. 闵涛 E_mail:admin@mintao.net(欢迎提供学习资源)
鄂公网安备 42011102001154号
站长:MinTao ICP备案号:鄂ICP备11006601号-18