Match Table w/ Regex

2019-08-10 19:47发布

I'm trying to match a table w/ regex but I'm having some issues. I can't figure out exactly why it will not match properly. Here is the 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>

I need to get 2 pieces of information: the data inside of the td below Queue and the data inside the td below Wait (so the Queue count and wait time). Obivously the numbers are going to update frequently.

This is the regex I have for pulling the initial table, but it isnt working:

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

And I'm not sure what regex I should use to get the data from the td's.

Before anyone asks, no there is no way I can update the HTML to have an ID or anything of that nature. Its pretty much as is. The only thing that is consistent is the location of the td's.

1条回答
迷人小祖宗
2楼-- · 2019-08-10 19:51

Instead of regex, I suggest using the HTML Agility Pack to parse the HTML and query its structure.

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).

In general, regex is a poor choice for parsing HTML.

查看更多
登录 后发表回答