0

我试图让我的控制台应用程序在 html 网页中返回所有这些值: <img border="0" alt="img.jpg" title="img.jpg" src="/_layouts/images/icjpg.gif" />

不过我只对title="img.jpg". 我已经看到可以使用正则表达式来过滤这些,但是我看不到我如何过滤掉其余的,只保留title="img.jpg"或中的值title="THIS"

任何帮助,将不胜感激。

我看过这个正则表达式的备忘单

4

2 回答 2

7

更好的解决方案是使用 HtmlAgilityPack 来解析 HTML。

http://htmlagilitypack.codeplex.com/

示例 [仅带有边框 =“0”的 img 标签]

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(your html string);

List<String> titles = (from x in doc.DocumentNode.Descendants()
                       where x.Name == "img"
                       && x.Attributes["title"] != null
                       && x.Attributes["border"] != null
                       && x.Attributes["border"].Value == "0"
                       select x.Attributes["title"].Value).ToList<String>();

此 LINQ 代码返回所有 img 标记的标题。

于 2012-11-07T10:42:24.513 回答
2

尝试使用codeplex中的HtmlAglityPack。或者你可以试试这个正则表达式

<img[^>]*(?<title>title=\"[^\"]+\")[^>]*>

以及使用 LINQ 的示例代码:

var result = from Match match 
             in Regex.Matches(strInput, "<img[^<]*(?<title>title=\"[^\"]+\")[^<]*>")
             select match.Groups["title"].Value;
于 2012-11-07T10:43:54.290 回答