考虑下面的HTML里面......C#正则表达式抢到2个信息从每个HTML元素的TR - 位于不同的TD元素
<table>
<tr>
<td><strong>Name 1</strong></td>
<td>Info and ignore <a href="/gohere"/>this</a></td>
<td><a href="MySpecialAction?field=&list=10000">Edit</a></td>
</tr>
<tr>
<td><strong>Name 2</strong></td>
<td>Info and ignore <a href="/gohere"/>this</a></td>
<td><a href="MySpecialAction?field=&list=10001">Edit</a></td>
</tr>
</table>
是否可以写一个C#正则表达式那会抢'name'(与td/strong一起找到)和'listid'(在包含MySpecialAction的href中找到)?
我抓住了它的名字(可能效率不高,但我希望我可以写一个表达式,如上所述,会有2个匹配,每个匹配将有两个组(命名为'name'和'id “)。
<strong\b[^>]*>(.*?)<\/strong>
Match1.name=Name 1
Match1.id=10000
Match2.name=Name 2
Match2.id=10001
在此先感谢。
为什么不使用html解析器? – spender 2014-11-25 08:54:01
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags。不要使用正则表达式来解析html! – mybirthname 2014-11-25 08:55:14
@spender请指教一下'html parser'是什么?我并不是想要解析整个HTML体,而是从html中抽取几个字符串。我把html看作是一个简单的'大字符串'和Regex作为匹配'string'部分的工具。很明显,我在这些领域的知识水平偏低。很高兴使用最简单的方法(并且非常简单地阅读,我从来没有花时间学习正则表达式语法)是正确的工具。 – Terry 2014-11-25 15:10:49