批量正则替换某文件夹中代码(div id=XXXX替换为div id="XXXX")
遍历文件夹中的指定文件名文件(html,htm)利用正则表达式批量替换其中的可变内容


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
private string[] filename = new string[2];
//private string fullname;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void replace(string str)
{
Regex reg = new Regex("id=(([a-z-A-Z]|-|_)+)>");
string newtext = reg.Replace(str, "id=\"$1\">");
Response.Write(newtext);
}
/*protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
filepath = FileUpload1.PostedFile.FileName;
fullname = FileUpload1.FileName;
filename = fullname.Split('.');
readFile(filepath);
}
if (FileUpload1.HasFile)
{
string filepath = "C:\\Documents and Settings\\第一页23\\桌面\\" + this.FileUpload1.PostedFile.FileName.ToString() ;
readFile(filepath);
}
}*/
protected void readFile(string filepath)
{
StreamReader sr = new StreamReader(filepath, System.Text.Encoding.Default);
try
{
string str = sr.ReadToEnd();
replace(str);
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
finally
{
sr.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string path = dir.Text.Substring(0,dir.Text.LastIndexOf("\\")+1);
// Response.Write(path);
// Response.End();
string[] files = Directory.GetFiles(path);
foreach (string f in files)
{
//Response.Write(f);
if(f.IndexOf(".html")>0 || f.IndexOf(".htm")>0)
{
Response.Write("now:changing" + f);
readFile(f);
}
}
}
}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script>
function filltext()
{
document.getElementById("dir").innerText=document.getElementById("file1").value;
//alert()
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="file" id="file1" />
<input type="button" value=" fill " OnClick="filltext()" />
<asp:TextBox ID="dir" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="submit" OnClick="Button2_Click" /></div>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
private string[] filename = new string[2];
//private string fullname;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void replace(string str)
{
Regex reg = new Regex("id=(([a-z-A-Z]|-|_)+)>");
string newtext = reg.Replace(str, "id=\"$1\">");
Response.Write(newtext);
}
/*protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
filepath = FileUpload1.PostedFile.FileName;
fullname = FileUpload1.FileName;
filename = fullname.Split('.');
readFile(filepath);
}
if (FileUpload1.HasFile)
{
string filepath = "C:\\Documents and Settings\\第一页23\\桌面\\" + this.FileUpload1.PostedFile.FileName.ToString() ;
readFile(filepath);
}
}*/
protected void readFile(string filepath)
{
StreamReader sr = new StreamReader(filepath, System.Text.Encoding.Default);
try
{
string str = sr.ReadToEnd();
replace(str);
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
finally
{
sr.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string path = dir.Text.Substring(0,dir.Text.LastIndexOf("\\")+1);
// Response.Write(path);
DirectoryInfo dif = new DirectoryInfo(path);
forFileLength(dif);
Response.End();
string[] files = Directory.GetFiles(path);
foreach (string f in files)
{
//Response.Write(f);
if(f.IndexOf(".html")>0 || f.IndexOf(".htm")>0)
{
Response.Write("now:changing" + f);
readFile(f);
}
}
}
private void forFileLength(DirectoryInfo directory)
{
DirectoryInfo[] directorys = directory.GetDirectories();
FileInfo[] files;
foreach (DirectoryInfo di in directorys)
{
forFileLength(di);
}
files = directory.GetFiles();
foreach(FileInfo file in files)
{
string temp = file.DirectoryName.ToString();
// temp 当前路径(除文件名)
string name = file.Name;
if (name.IndexOf(".html") > 0 || name.IndexOf(".htm") > 0)
{
Response.Write("now:changing: " + name+"<BR/>");
//readFile(f);
}
}
}
}