如何在c#正則表達式中通過id獲取td的值?

[英]How to get value of td by id in c# regular expression?


I have following html, there more td's but have put only few of below. I want to get value of value of TD which has id "hdNumber" through c# code.I want to use regular expression. sometime when come generate html from window live for (email) it may be possible that it render html like "8332 without quotation marks before and after id. I want to get number 8332 only.

我有跟隨html,有更多的td,但只放了幾個以下。我希望通過c#代碼獲得具有id“hdNumber”的TD的值。我想使用正則表達式。有時從窗口生成html為(電子郵件)生成html,它可能會呈現html,如“8332在id之前和之后沒有引號。我想只得到號碼8332。

<table>
<tr>
    <TD style="COLOR: #666" vAlign=top>
         Good<TD>
       <TD id="hdNumber"
       style="BACKGROUND: white; COLOR: white; DISPLAY: none">8332
    </TD> 
</tr>
</table>

2 个解决方案

#1


Don't use regex to parse HTML. You can use HtmlAgilityPack:

不要使用正則表達式來解析HTML。您可以使用HtmlAgilityPack:

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlString);
var hdNumber = doc.GetElementbyId("hdNumber");
if(hdNumber != null)
{
    string number = hdNumber.InnerText.Trim('\r', '\n', ' ', '"');  // 8332
}

I have used Trim('\r', '\n', ' ', '"') to remove possible leading and trailing spaces, newline characters and quotes as desired.

我使用Trim('\ r','\ n','','“')來刪除可能的前導和尾隨空格,換行符和引號。

#2


I want to use regular expression.

我想使用正則表達式。

You can use the following with s modifier or DOTALL if you don't want to use DOM Parser (recommended) :

如果您不想使用DOM Parser(推薦),可以將以下內容與s修飾符或DOTALL一起使用:

<TD\s*id\s*=\s*"?hdNumber"?.*?>(.*?)</TD>

And extract number with $1

並以1美元提取數字

See DEMO


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/11/729d2138e42209c5702d9c46232b1a83.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com