用于 html 解析的正则表达式(在 C# 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/271741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
regex for html parsing (in c#)
提问by
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 (oC)" class="TABLEDATACELL" nowrap="nowrap" align="Left" colspan="1" rowspan="1">Max Temperature (oC)</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.
但是我似乎无法提取任何匹配项。谁能指出我正确的方向,谢谢。
采纳答案by Stefan Gehrig
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*
回答by siukurnin
When you write <td[^<]+?>
I guess you really mean <td[^>]*>
当你写的时候<td[^<]+?>
我猜你是真的意思<td[^>]*>
That is "opening brace, td, maybe stuff other than closingbrace..."
那是“开大括号,td,也许是闭大括号以外的东西......”
回答by The Archetypal Paul
<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.
还没有全部看过,但是 [^<] 可能需要是 [^>] ,因为您试图匹配所有非>,直到在最高温度之前的 > 。
回答by chakrit
The " (oC)" before the closing td was matched against:
结束 td 之前的 " (oC)" 匹配:
<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.
那是\wa 字边界吗?我认为那里有点棘手,我会使用更通用的方法。
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>
回答by Bjarke Ebert
Parsing HTML reliably using regexp is known to be notoriously difficult.
众所周知,使用正则表达式可靠地解析 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 处理是安全攻击的主要来源。
回答by yusuf
I use http://www.regexbuddy.com/for such controls. So far I tested @sgehrig's suggestion is correct
我使用http://www.regexbuddy.com/进行此类控制。到目前为止,我测试了@sgehrig 的建议是正确的
回答by TrueWill
Use the Html Agility Packor a similar library instead, as @Bjarke Ebert suggests. It's the right tool for the task.
正如@Bjarke Ebert 所建议的那样,请改用Html Agility Pack或类似的库。这是完成任务的正确工具。