正则问题,请指教!
本帖最后由 Aspclass 于 2013-12-23 18:21:11 编辑 http://www.scflcp.com/trend/analysekl12.do?operator=history&pageCount=10
这个网址,我弄了好久了,都弄不出来,请大侠些帮个忙。
我要列表里面的,期号和开奖号码,这两样就可以了。
我的匹配总是出错。
错误提示:限定符 {x,y} 前没有任何内容。
<td>2013(*.?)</td> <td>(*.?)</td>
模块:
Function getHTMLCode(ByVal Url As String, ByVal Encoding As String) As String
Try
Dim wc As Net.WebClient = New Net.WebClient()
wc.Credentials = Net.CredentialCache.DefaultCredentials
wc.Headers.Set("User-Agent", "Microsoft Internet Explorer") '//增加的代码
Dim resStream As IO.Stream = wc.OpenRead(Url)
Dim sr As IO.StreamReader = New IO.StreamReader(resStream, System.Text.Encoding.GetEncoding(Encoding))
Dim code As String = sr.ReadToEnd()
resStream.Close()
getHTMLCode = code
Catch ex As Exception
getHTMLCode = ""
End Try
End Function
Public Function GetResult() As String
Dim CurrURL As String
Dim code As String = ""
Dim myMatchCollection As MatchCollection
Dim myMach As Match
Dim str As String
str = "<td>2013(*.?)</td> <td>(*.?)</td>"
Dim i As Integer = 0
GetResult = ""
CurrURL = "http://www.scflcp.com/trend/analysekl12.do?operator=history&pageCount=10"
code = code & getHTMLCode(CurrURL, "gb2312")
myMatchCollection = Regex.Matches(code, str)
For Each myMach In myMatchCollection
GetResult = myMach.Groups(1).ToString & "|" & myMach.Groups(2).ToString
Next
End Function
执行:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(GetResult())
End Sub
请各位帮我看看、、、谢谢了。
VS版本,2010
[解决办法]
<td>2013(.*?)</td> <td>(.*?)</td>
你想要的是这样的吧
[解决办法]
<tr>\s*?<td>(?<id>\d+)\D+(?<ball>[^<]+)
取分组id和分组ball
for each m as Match in Regex.Matches(yourHtmlString,"<tr>\s*?<td>(?<id>\d+)\D+(?<ball>[^<]+)")
m.Groups("id").Value'这个就是期号
m.Groups("ball").Value'这个就是中奖号
next