【发布时间】:2013-06-25 22:38:48
【问题描述】:
我有一长串 HTML 代码的 c# 字符串,我想专门提取项目符号“<ul><li></li></ul>”。
假设我有以下 HTML 字符串。
var html = "<div class=ClassC441AA82DA8C5C23878D8>Here is a text that should be ignored.</div>This text should be ignored too<br><ul><li>* Need this one</li><li>Another bullet point I need</li><li>A bulletpoint again that I want</li><li>And this is the last bullet I want</li></ul><div>Ignore this line and text</div><p>Ignore this as well.</p>Text not important."
我需要“<ul>”到“</ul>”标签之间的所有内容。可以排除“<ul>”标签。
现在正则表达式不是我最强的一面,但如果可以使用,我需要一些帮助。 我的代码在 c# 中。
【问题讨论】:
-
你有什么尝试吗?看看 Html Agility Pack。
-
使用解析器,例如HTML Agility Pack,而不是正则表达式。
-
你真的应该阅读这个答案:D stackoverflow.com/questions/1732348/…
-
并且(再次严肃)这篇文章codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html 和这个答案(解释原因)stackoverflow.com/a/590789/863564
-
第二条评论是相关的.. 但第一个答案让我每次都崩溃:) 抱歉,我忍不住链接到它,这有点 SO 历史