六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 62|回复: 0

匹配html中的所有A标签并替换

[复制链接]

升级  27.33%

23

主题

23

主题

23

主题

秀才

Rank: 2

积分
91
 楼主| 发表于 2013-2-1 09:49:00 | 显示全部楼层 |阅读模式
匹配html中的A标签
<[aA]\s+((\w+\s*=\s*("([^"]*)"|'([^']*)'|([^'">\s]+))\s+)*)(href\s*=\s*("([^"]*)"|'([^']*)'|([^'">\s]+)))((\s+\w+\s*=\s*("([^"]*)"|'([^']*)'|([^'">\s]+)))*)\s*>(.*?)</[aA]\s*>
替换其中的链接到统计页面再跳转到真正的链接地址
        protected void Unnamed1_Click(object sender, EventArgs e)    {        //正则        Regex reg = new Regex("<[aA]\\s+((\\w+\\s*=\\s*(\"([^\"]*)\"|'([^']*)'|([^'\">\\s]+))\\s+)*)(href\\s*=\\s*(\"([^\"]*)\"|'([^']*)'|([^'\">\\s]+)))((\\s+\\w+\\s*=\\s*(\"([^\"]*)\"|'([^']*)'|([^'\">\\s]+)))*)\\s*>(.*?)</[aA]\\s*>");                //输入内容        string matchStr = TextBox1.Text;         //此处可以完成任务(利用委托)        MatchEvaluator myEvaluator = new MatchEvaluator(ReplaceCC);        string aaa = reg.Replace(matchStr, myEvaluator);        //最后结果        Response.Write(aaa);    }//在这里单独处理每一个匹配项    public string ReplaceCC(Match m)    {        string temp = m.Groups[9].Value + m.Groups[10].Value + m.Groups[11].Value;        string g18 = m.Groups[18].Value;        temp = "http://www.biaodashi.com?projectid=00000&userid=1111111&url=" + temp;        return "<a " + m.Groups[1].Value + " href=\" " + temp + "\" " + m.Groups[12].Value + ">" + g18 + "</><br/>";    }

实例:http://dl.iteye.com/topics/download/41e998cf-b147-3236-9c7e-c9573924b375
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表