我需要从一个HTML字符串中解析出特定的部分。例如:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>[section=quote]</p>
<p>Mauris at turpis nec dolor bibendum sollicitudin ac quis neque.</p>
<p>[/section]</p>
解析引用部分应返回:
<p>Mauris at turpis nec dolor bibendum sollicitudin ac quis neque.</p>
目前我正在使用正则表达式来获取 [section=quote]...[/section] 内的内容,但由于这些部分是使用所见即所得编辑器输入的,因此部分标记本身被包装在段落标记中,因此解析的结果是:
</p>
<p>Mauris at turpis nec dolor bibendum sollicitudin ac quis neque.</p>
<p>
我现在使用的正则表达式是:
\[section=(.+?)\](.+?)\[/section\]
在解析部分之前,我还要进行一些额外的清理工作:
protected string CleanHtml(string input) {
// remove whitespace
input = Regex.Replace(input, @"\s*(<[^>]+>)\s*", "$1", RegexOptions.Singleline);
// remove empty p elements
input = Regex.Replace(input, @"<p\s*/>|<p>\s*</p>", string.Empty);
return input;
}
有没有人能提供一个正则表达式来实现我要找的东西,或者说我试图用正则表达式做这个事情是浪费时间吗?我看到了 Html Agility Pack 的参考资料 - 对于这样的事情,这个工具会更好吗?
[更新]
感谢 Oscar,我已经使用了 HTML Agility Pack 和正则表达式的组合来解析这些部分。它仍需要一些细化,但已经接近完成。
public void ParseSections(string content)
{
this.SourceContent = content;
this.NonSectionedContent = content;
content = CleanHtml(content);
if (!sectionRegex.IsMatch(content))
return;
var doc = new HtmlDocument();
doc.LoadHtml(content);
bool flag = false;
string sectionName = string.Empty;
var sectionContent = new StringBuilder();
var unsectioned = new StringBuilder();
foreach (var n in doc.DocumentNode.SelectNodes("//p")) {
if (startSectionRegex.IsMatch(n.InnerText)) {
flag = true;
sectionName = startSectionRegex.Match(n.InnerText).Groups[1].Value.ToLowerInvariant();
continue;
}
if (endSectionRegex.IsMatch(n.InnerText)) {
flag = false;
this.Sections.Add(sectionName, sectionContent.ToString());
sectionContent.Clear();
continue;
}
if (flag)
sectionContent.Append(n.OuterHtml);
else
unsectioned.Append(n.OuterHtml);
}
this.NonSectionedContent = unsectioned.ToString();
}