【问题标题】:Get Div Content with Regular Expressions in C#在 C# 中使用正则表达式获取 div 内容
【发布时间】:2011-06-21 11:47:46
【问题描述】:

我有这个 Html 代码:

<div id="top" style="something i dont know">
Text
</div>

我只想得到字符串“Text”。 我的脚本如下所示:

Regex search_string = new Regex("<div id=\"top\".*?>([^<]+)</div>");
Match match = search_string.Match(code);
string section = match.Groups[0].Value;
MessageBox.Show(section);

这甚至可以用 C# 实现吗?

【问题讨论】:

标签: c# regex text html


【解决方案1】:

您最好使用前面提到的 XPath。为了能够像使用 XML 一样使用 HTML,您可以使用 HTML Agility Pack,这对于像您这样的任务非常有用。

【讨论】:

    【解决方案2】:

    使用 XPath 更容易

    http://www.codeproject.com/KB/cpp/myXPath.aspx

    将其用作 xpath 选择器

    //div[@id='top']
    

    那么你可以获得内在价值

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 2013-05-02
      • 1970-01-01
      相关资源
      最近更新 更多