用于html解析的正则表达式(在c#中)

时间:2022-10-29 16:54:15

I'm trying to parse a html page and extract 2 values from a table row. The html for the table row is as follows: -

我正在尝试解析html页面并从表格行中提取2个值。表行的html如下: -

<tr>
<td title="Associated temperature in (ºC)" class="TABLEDATACELL" nowrap="nowrap" align="Left" colspan="1" rowspan="1">Max Temperature (ºC)</td>
<td class="TABLEDATACELLNOTT" nowrap="nowrap" align="Center" colspan="1" rowspan="1">6</td>
<td class="TABLEDATACELLNOTT" nowrap="nowrap" align="Center" colspan="1" rowspan="1"> 13:41:30</td>
</tr>

and the expression I have at the moment is:

而我现在的表达是:

<tr>[\s]<td[^<]+?>Max Temperature[\w\s]*</td>[\s]
<td[^<]+?>(?<value>([\d]+))</td>[\s]
<td[^<]+?>(?<time>([\d\:]+))</td>[\s]</tr>

However I don't seem to be able to extract any matches. Could anyone point me in the right direction, thanks.

但是我似乎无法提取任何匹配项。任何人都可以指出我正确的方向,谢谢。

7 个解决方案

#1


1  

Try

<tr>\s*
<td[^>]*>.*?</td>\s*
<td[^>]*>\s*(?<value>\d+)\s*</td>\s*
<td[^>]*>\s*(?<time>\d{2}:\d{2}:\d{2})\s*</td>\s*
</tr>\s*

#2


4  

Parsing HTML reliably using regexp is known to be notoriously difficult.

众所周知,使用regexp可靠地解析HTML是非常困难的。

I think I would be looking for a HTML parsing library, or a "screen scraping" library ;)

我想我会寻找一个HTML解析库,或“屏幕抓取”库;)

If the HTML comes from an unreliable source, you have to be extra careful to handle malicious HTML syntax well. Bad HTML handling is a major source of security attacks.

如果HTML来自不可靠的源,则必须格外小心处理恶意HTML语法。糟糕的HTML处理是安全攻击的主要来源。

#3


0  

When you write <td[^<]+?> I guess you really mean <td[^>]*>

当你写我猜你真的是指] *>

That is "opening brace, td, maybe stuff other than closing brace..."

那是“开口支撑,td,也许是闭合支撑以外的其他东西......”

#4


0  

<tr>[\s]<td[^<]+?>Max Temperature[\w\s]*</td>[\s]

Not looked at it all yet, but that [^<] probably needs to be [^>] as you're trying to match all non-> until the > that's before Max temperature.

还没有看完它,但是[^ <]可能需要[^>],因为你试图匹配所有非>直到>在最高温度之前。

#5


0  

The " (ºC)" before the closing td was matched against:

关闭td之前的“(ºC)”与:

<tr>[\s]<td[^<]+?>Max Temperature[^<]*</td>[\s]

Is that \w a word-boundary? I think that it gets a little tricky there, I'd use a more general approach.

这是一个字边界吗?我认为那里有点棘手,我会采用更通用的方法。

And on the third line, there is one whitespace after the td tag, is that accounted for?

在第三行,td标签后面有一个空格,是否占了?

<td[^<]+?>[\s]?(?<time>([\d\:]+))</td>[\s]</tr>

#6


0  

I use http://www.regexbuddy.com/ for such controls. So far I tested @sgehrig's suggestion is correct

我使用http://www.regexbuddy.com/进行此类控制。到目前为止,我测试了@ sgehrig的建议是正确的

#7


0  

Use the Html Agility Pack or a similar library instead, as @Bjarke Ebert suggests. It's the right tool for the task.

正如@Bjarke Ebert建议的那样,使用Html Agility Pack或类似的库。它是完成任务的正确工具。

#1


1  

Try

<tr>\s*
<td[^>]*>.*?</td>\s*
<td[^>]*>\s*(?<value>\d+)\s*</td>\s*
<td[^>]*>\s*(?<time>\d{2}:\d{2}:\d{2})\s*</td>\s*
</tr>\s*

#2


4  

Parsing HTML reliably using regexp is known to be notoriously difficult.

众所周知,使用regexp可靠地解析HTML是非常困难的。

I think I would be looking for a HTML parsing library, or a "screen scraping" library ;)

我想我会寻找一个HTML解析库,或“屏幕抓取”库;)

If the HTML comes from an unreliable source, you have to be extra careful to handle malicious HTML syntax well. Bad HTML handling is a major source of security attacks.

如果HTML来自不可靠的源,则必须格外小心处理恶意HTML语法。糟糕的HTML处理是安全攻击的主要来源。

#3


0  

When you write <td[^<]+?> I guess you really mean <td[^>]*>

当你写我猜你真的是指] *>

That is "opening brace, td, maybe stuff other than closing brace..."

那是“开口支撑,td,也许是闭合支撑以外的其他东西......”

#4


0  

<tr>[\s]<td[^<]+?>Max Temperature[\w\s]*</td>[\s]

Not looked at it all yet, but that [^<] probably needs to be [^>] as you're trying to match all non-> until the > that's before Max temperature.

还没有看完它,但是[^ <]可能需要[^>],因为你试图匹配所有非>直到>在最高温度之前。

#5


0  

The " (ºC)" before the closing td was matched against:

关闭td之前的“(ºC)”与:

<tr>[\s]<td[^<]+?>Max Temperature[^<]*</td>[\s]

Is that \w a word-boundary? I think that it gets a little tricky there, I'd use a more general approach.

这是一个字边界吗?我认为那里有点棘手,我会采用更通用的方法。

And on the third line, there is one whitespace after the td tag, is that accounted for?

在第三行,td标签后面有一个空格,是否占了?

<td[^<]+?>[\s]?(?<time>([\d\:]+))</td>[\s]</tr>

#6


0  

I use http://www.regexbuddy.com/ for such controls. So far I tested @sgehrig's suggestion is correct

我使用http://www.regexbuddy.com/进行此类控制。到目前为止,我测试了@ sgehrig的建议是正确的

#7


0  

Use the Html Agility Pack or a similar library instead, as @Bjarke Ebert suggests. It's the right tool for the task.

正如@Bjarke Ebert建议的那样,使用Html Agility Pack或类似的库。它是完成任务的正确工具。