【发布时间】:2012-07-03 07:48:00
【问题描述】:
这里有点头脑僵硬,所以我希望得到一些指示,基本上我需要提取特定 div 标签的内容,是的,我知道正则表达式通常不被批准,但它是一个简单的网络没有嵌套 div 的抓取应用程序。
我正在尝试匹配这个:
<div class="entry">
<span class="title">Some company</span>
<span class="description">
<strong>Address: </strong>Some address
<br /><strong>Telephone: </strong> 01908 12345
</span>
</div>
简单的vb代码如下:
Dim myMatches As MatchCollection
Dim myRegex As New Regex("<div.*?class=""entry"".*?>.*</div>", RegexOptions.Singleline)
Dim wc As New WebClient
Dim html As String = wc.DownloadString("http://somewebaddress.com")
RichTextBox1.Text = html
myMatches = myRegex.Matches(html)
MsgBox(html)
'Search for all the words in a string
Dim successfulMatch As Match
For Each successfulMatch In myMatches
MsgBox(successfulMatch.Groups(1).ToString)
Next
任何帮助将不胜感激。
【问题讨论】:
-
您使用的正则表达式有什么问题?它与您的输入相匹配。
-
这很奇怪,它与整个页面上的任何内容都不匹配,并且那里大约有 20 个 div
-
我知道@Tim 以比我更好的方式回答了这个问题,但是为了您将来的参考,没有第二组,所以
Groups(1)(这是 base-0 索引)将总是返回一个空字符串......它应该是Groups(0)