用正则表达式除去行号
用正则表达式去掉行号
=====文件名:RegExDemoMain.cpp=====
/***************************************************************
* Name: RegExDemoMain.cpp
* Purpose: Code for Application Frame
* Author: emonkey (dqifa@163.com)
* Created: 2012-10-08
* Copyright: emonkey (http://emonkey.iteye.com/)
* License:
**************************************************************/
#include <wx/regex.h>
#include <wx/textfile.h>
#include <wx/log.h>
#include <wx/dcclient.h>
void RegExDemoFrame::OnAbout(wxCommandEvent& event)
{
wxRegEx r;
wxTextFile fin;
wxTextFile fout("outfile.txt");
fout.Create();
r.Compile("\\D*\\d+\\s?[.|:|\\]|\\)]?\\s?",wxRE_ADVANCED);
fin.Open("testfile.txt",wxConvAuto(wxFONTENCODING_CP936));
//要加上wxConvAuto(wxFONTENCODING_CP936),否则中文会出现乱码
if(fin.IsOpened())
{
wxString line,s;
for(line=fin.GetFirstLine(); !fin.Eof(); line=fin.GetNextLine())
{
if(r.Matches(line))
{
// wxMessageBox(r.GetMatch(line));
r.Replace(&line,"",1);
}
s<<line<<"\n";
fout.AddLine(line);
}
//上面调用AddLine()方法并未将数据写入物理文件,所以一定要调用
//Write( wxTextFileType typeNew = wxTextFileType_None,
//const wxMBConv & conv = wxConvAuto() )方法,该方法调用成功时返回true.
if(fout.Write(/*wxTextFileType_None,wxConvAuto(wxFONTENCODING_CP936)*/))
{
wxLogStatus("“testfile.txt”文件到“outfile.txt”文件转换已完成!");
// wxLogStatus("文件转换已完成!");
}
fout.Close();
fin.Close();
//显示转换后的文件
wxClientDC dc(this);
wxFont font;
font.SetPointSize(12);
dc.SetFont(font);
dc.SetTextForeground(wxColour(128,0,0));
dc.DrawText(s,20,10);
}
}
参考:
1、Python正则表达式如何删除代码行
http://www.wv168.com/HTML/PYTHON/2012/0627/6149.html
2、wxWidgets文件操作(二)wxTextFile
http://www.cnblogs.com/godspeedsam/archive/2011/03/16/1986302.html