需要更换锚标签的HREF字符串中的 [英] Need to replace href of anchor tags in a string

查看:120
本文介绍了需要更换锚标签的HREF字符串中的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string content=" 
        <br /><br /><a href="need to replace this url">Cooking School</a><br /><br /><a href="http://www.sdlm.com">Feed your senses</a><br /><br /><a href="http://www.sdl.com">Take your cooking skills to the next level. Find a cooking school near you!</a><br /><br /><a href="http:google.com"><img src="http://www.sdlm1.com/autd3umrl_u_t.jpg" /></a>
     "

我需要全部更换锚标记的href网址不同的
值我用下面的功能,但它得到错误

I need to replace all anchor tags href value with different urls I used the following function but its getting error

 public List<string> GetLinksFromHtml(string content)
        {
            string regex = @"<(?<Tag_Name>(a)|img)\b[^>]*?\b(?<URL_Type>(?(1)href|src))\s*=\s*(?:""(?<URL>(?:\\""|[^""])*)""|'(?<URL>(?:\\'|[^'])*)'))";
            var matches = Regex.Matches(content, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            var links = new List<string>();

            foreach (Match item in matches)
            {
                string link = item.Groups[1].Value;
                links.Add(link);
            }

            return links;
        }



感谢您的帮助。

Thanks for any help

推荐答案

试图用正则表达式解析HTML是不是一个好主意。请参见这个帖子。使用真正的HTML解析器像 HtmlAgilityPack

Trying to parse html with regex is not a good idea. See this post. Use a real html parser like HtmlAgilityPack .

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(content);
foreach (var a in doc.DocumentNode.Descendants("a"))
{
    a.Attributes["href"].Value = "http://a.com?url=" + HttpUtility.UrlEncode(a.Attributes["href"].Value);
}

var newContent = doc.DocumentNode.OuterHtml;

这篇关于需要更换锚标签的HREF字符串中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆