高手快帮忙,关于replace和正者表达式的问题

本文介绍了一种使用C#编程语言处理HTML文档的方法,具体为如何将HTML中的<a href=属性值进行统一替换,同时解决了替换过程中遇到的双引号问题。

我想把我获取到的HTML里的所有的所有的<a href="替换成<a href="default2.aspx?str=,但是里面有双引号,想问一下怎么用正者表达转换啊
看下面代码

 1None.gifusing System;
 2None.gifusing System.Data;
 3None.gifusing System.Configuration;
 4None.gifusing System.Collections;
 5None.gifusing System.Web;
 6None.gifusing System.Web.Security;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.WebControls.WebParts;
10None.gifusing System.Web.UI.HtmlControls;
11None.gifusing System.Net;
12None.gifusing System.Drawing;
13None.gifusing System.Drawing.Imaging;
14None.gifusing System.IO;
15None.gif
16None.gif
17None.gifpublic partial class Default2 : System.Web.UI.Page
18ExpandedBlockStart.gifContractedBlock.gifdot.gif{
19InBlock.gif    protected void Page_Load(object sender, EventArgs e)
20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
21InBlock.gif        GetWeather();
22ExpandedSubBlockEnd.gif    }

23InBlock.gif    private void GetWeather()
24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
25InBlock.gif        string str = "http://z.zhongsou.com/gn.htm";
26InBlock.gif        try
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            str = Request.Params["str"].ToString();
29InBlock.gif
30ExpandedSubBlockEnd.gif        }

31InBlock.gif        catch
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif 
34ExpandedSubBlockEnd.gif        }

35InBlock.gif            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str);
36InBlock.gif        request.Method = "Get";
37InBlock.gif        request.ContentType = "application/x-www-form-urlencoded";
38InBlock.gif        WebResponse response = request.GetResponse();
39InBlock.gif        Stream s = response.GetResponseStream();
40InBlock.gif        StreamReader sr = new StreamReader(s, System.Text.Encoding.GetEncoding("GB2312"));
41InBlock.gif        string html = sr.ReadToEnd();
42InBlock.gif        s.Close();
43InBlock.gif        sr.Close();
44InBlock.gif        string htm = html.Replace("<a href=""<a href='default2.aspx?str=");//我想把我获取到的HTML里的所有的所有的<a href="替换成<a href="default2.aspx?str=,但是里面有双引号,想问一下怎么用正者表达转换啊
45InBlock.gif        System.IO.StreamWriter sw;
46InBlock.gif        string name = System.DateTime.Now.Date.ToShortDateString()+ System.DateTime.Now.Year.ToString() + System.DateTime.Now.Second.ToString();
47InBlock.gif        sw = new System.IO.StreamWriter(Server.MapPath(name + ".htm"), false, System.Text.Encoding.GetEncoding("GB2312"));
48InBlock.gif        sw.Write(htm);
49InBlock.gif        sw.Close();
50InBlock.gif      //  Response.Write(html);
51InBlock.gif        Response.Write(html);
52InBlock.gif
53ExpandedSubBlockEnd.gif    }

54ExpandedBlockEnd.gif}

55None.gif

转载于:https://www.cnblogs.com/sujingnan/archive/2008/03/19/1113382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值