【发布时间】:2018-05-18 13:26:57
【问题描述】:
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>
"
我需要用不同的 url 替换所有锚标签的 href 值 我使用了以下功能,但出现错误
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;
}
感谢您的帮助
【问题讨论】:
-
我只想获取所有锚标签href值,以便可以用我想要的其他网址替换它们。当我在堆栈溢出中搜索时,我得到了上面的函数,我刚刚尝试过,但错误是 --> parsing "(a)|img)\b[^>]*?\b(?
( ?(1)href|src))\s*=\s*(?:"(? (?:\\"|[^"])*)"|'(? (?: \\'|[^'])*)'))" - ) 太多。