c# - 带正则表达式的匹配表

标签 c# .net regex .net-4.0

我正在尝试使用正则表达式匹配表格,但我遇到了一些问题。我不明白为什么它不能正确匹配。这是 HTML:

    <table class="integrationteamstats">
    <tbody>
    <tr>
        <td class="right">
            <span class="mediumtextBlack">Queue:</span>
        </td>
        <td class="left">
            <span class="mediumtextBlack">0</span>
        </td>
        <td class="right">
            <span class="mediumtextBlack">Aban:</span>
        </td>
        <td class="left">
            <span class="mediumtextBlack">0%</span>
        </td>
        <td class="right">
            <span class="mediumtextBlack">Staffed:</span>
        </td>
        <td class="left">
            <span class="mediumtextBlack">0</span>
        </td>
    </tr>
    <tr>
        <td class="right">
            <span class="mediumtextBlack">Wait:</span>
        </td>
        <td class="left">
            <span class="mediumtextBlack">0:00</span>
        </td>
        <td class="right">
            <span class="mediumtextBlack">Total:</span>
        </td>
        <td class="left">
            <span class="mediumtextBlack">0</span>
        </td>
        <td class="right">
            <span class="mediumtextBlack">On ACD:</span>
        </td>
        <td class="left">
            <span class="mediumtextBlack">0</span>
        </td>
    </tr>
    </tbody>
    </table>

我需要获取两条信息: 队列下方的 td 内的数据和等待下方的 td 内的数据(因此队列计数和等待时间)。显然,这些数字会经常更新。

这是我用于提取初始表的正则表达式,但它不起作用:

Match statstable = Regex.Match(this.html, "<table class=\"integrationteamstats\">(.*?)</table>");

而且我不确定应该使用什么正则表达式从 td 获取数据。

在任何人询问之前,不,我无法更新 HTML 以具有 ID 或任何类似性质的内容。它几乎是这样。唯一一致的是 td 的位置。

最佳答案

我建议使用 HTML Agility Pack 而不是正则表达式解析 HTML 并查询其结构。

What is exactly the Html Agility Pack (HAP)?

This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).

一般来说,regex is a poor choice for parsing HTML .

关于c# - 带正则表达式的匹配表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13936841/

相关文章:

javascript - 返回给定 URL 中音频文件的最后一个文件夹名称

c - 无法匹配 C 中的正则表达式

regex - 匹配包含 *N* 次特定字母的单词

c# - 使用 Skype4ComLib 时出错

.net - 亚音速ActiveRecord和外键属性名称

c# - 如果数据未按给定偏移量对齐,为什么 BitConverter.ToInt32 一次读取一个字节?

c# - 当页面在单击按钮时加载动态内容时,页脚 CSS 不起作用

c# - 从 ViewModel 到 Custom-Control 到 ControlTemplate 上的控件的多阶段绑定(bind)不起作用

c# - 如何将图像保存到文件系统?

c# - 为什么使用 DataTemplates 时 ViewModel 的 ObservableCollection 没有显示在 View 中?