c# - 使用正则表达式的图像标签的 src

标签 c# regex

我需要通过对以下字符串使用 regex 来找到第一个 img 标签的 src。 怎么做?

><div dir="ltr" style="text-align: left;" trbidi="on"><div class="MsoNormal"
 style="background: white; line-height: 15.0pt; margin-bottom: .0001pt; margin-bottom: 0in; mso-outline-level: 2; vertical-align: baseline;"><div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-c-ugY7XUnYo/UoJtj0dzvKI/AAAAAAAAACA/qWtvYnP9wfc/s1600/Screen+shot+2013-11-12+at+10.03.25+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="257" src="http://1.bp.blogspot.com/-c-ugY7XUnYo/UoJtj0dzvKI/AAAAAAAAACA/qWtvYnP9wfc/s320/Screen+shot+2013-11-12+at+10.03.25+AM.png" width="320" /></a></div><h4><span style="background-color: transparent;">With over 150,000 pet care professionals in the United States, your ability to differentiate your business is critical to long-term sustainable growth.  By focusing on the customer experience you can gain the loyalty of prospective pet parents and continue to thrive with your current pack.</span><span style="background-color: transparent;">  </span><span style="background-color: transparent;">Below are 5 ways to differentiate your pet business so you have a leg up on your local competitors.</span></h4></div><div class="MsoNormal"><div

最佳答案

Don't use Regex to parse html .使用真正的 html 解析器,如 HtmlAgilityPack

var html = WebUtility.HtmlDecode(yourtext);
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var urls = doc.DocumentNode.SelectNodes("//img[@src]")
              .Select(img => img.Attributes["src"].Value)
              .ToList(); 

关于c# - 使用正则表达式的图像标签的 src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21721686/

相关文章:

regex - 使用 sed 在 cygwin 中替换 ""string"\1\"787"到 "string"\1\"787"

ruby - 匹配未转义的平衡定界符对

c# - 如何指定用泛型来验证这种情况?

c# - XDocument 使用 XML 注释创建 XElement

javascript - 如何从值 1 AM , 1 :30 AM and so on? 创建选择列表

java - Regex 类在 GWT 的服务器端不起作用

c# - 如何对同一数组中的随机位置执行多线程操作?

c# - 如何将 MemoryStream 写入 byte[]

c# - 多个 View 共享相同的数据与多个线程之间的双向数据绑定(bind)

javascript - 正则表达式匹配左括号 [ 后的所有字符