Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要替换字符串中锚定标记的href_C#_Regex - Fatal编程技术网

C# 需要替换字符串中锚定标记的href

C# 需要替换字符串中锚定标记的href,c#,regex,C#,Regex,我需要用不同的URL替换所有锚定标记href value 我使用了以下函数,但它的获取错误 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><

我需要用不同的URL替换所有锚定标记href value 我使用了以下函数,但它的获取错误

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>
     "

感谢您的帮助

尝试用正则表达式解析html不是一个好主意。看见使用真正的html解析器,如


试图用正则表达式解析html不是一个好主意。看见使用真正的html解析器,如


我只想获得所有锚定标记href值,这样我就可以用我想要的其他URL替换它们。当我在堆栈溢出中搜索时,我得到了上面的函数,我只是尝试了一下,但错误是->解析]*?\b??1href | src\s*=\s*?:\ \ \ \ \ \[^]*?:\ \ \ \'\ \ \'[^']*-太多了。我只想获取所有锚标记href值,以便我可以用我想要的其他URL替换它们。当我在堆栈溢出中搜索时,我得到了上面的函数,我只是尝试了一下,但错误是->解析]*?\b??1href | src\s*=\s*?:\ \ \ \ \ \[^]*?:\ \ \ \ \ \'[^']*-太多的s.iam没有得到HtmlAgilityPackdll@user1622436这是什么意思?你不能或者你不想要什么这么难?在回答中转到链接。单击下载并解压缩文件:是的,我已经完成了。但在每个文件夹中都有一个dll。那么我需要使用哪个dll,请告诉我me@user1622436Foldernames表示silverlight的.net版本为L。选择适合你的。我没有收到HtmlAgilityPackdll@user1622436这是什么意思?你不能或者你不想要什么这么难?在回答中转到链接。单击下载并解压缩文件:是的,我已经完成了。但在每个文件夹中都有一个dll。那么我需要使用哪个dll,请告诉我me@user1622436Foldernames表示silverlight的.net版本为L。选择任何适合你的。
 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;
        }
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;