提取IMG源我有一个字符串是这样的:正则表达式从字符串
<img width="1" height="1" alt="" src="http://row.bc.yahoo.com.link">
我应该有什么正则表达式在C#写提取它的SRC部分? (最终结果应该是“http://row.bc.yahoo.com.link”)
提取IMG源我有一个字符串是这样的:正则表达式从字符串
<img width="1" height="1" alt="" src="http://row.bc.yahoo.com.link">
我应该有什么正则表达式在C#写提取它的SRC部分? (最终结果应该是“http://row.bc.yahoo.com.link”)
如果你正在处理HTML,你最好使用HTML分析器,如HTML Agility Pack。
样品:
var doc = new HtmlDocument();
doc.LoadHtml(
"<img width=\"1\" height=\"1\" alt=\"\" src=\"http://row.bc.yahoo.com.link\">");
var anchor = doc.DocumentNode.Element("img");
Console.WriteLine(anchor.Attributes["src"].Value);
更新: 如果您已经使用HTML敏捷性包,并选择从文档使用XPath需要迭代它们并访问所有img
标签src
属性:
var imgs = doc.DocumentNode.SelectNodes("//img/@src");
foreach (var node in imgs)
{
Console.WriteLine(node.Attributes["src"].Value);
}
你到现在为止有什么想法? –