批量正则替换某文件夹中代码(div id=XXXX替换为div id="XXXX")

遍历文件夹中的指定文件名文件(html,htm)利用正则表达式批量替换其中的可变内容
ContractedBlock.gif ExpandedBlockStart.gif cs
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);
            }
        }
    }
}

ContractedBlock.gif ExpandedBlockStart.gif aspx
<%@ 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>

ContractedBlock.gif ExpandedBlockStart.gif 改良的cs可遍历文件夹及其子文件夹
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:&nbsp;" + name+"<BR/>");
                    
//readFile(f);
                   
                }                                                    
            }  
    }   


}

转载于:https://www.cnblogs.com/daiye/archive/2009/09/16/1567431.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值