2010年vb.net中的正则表达式
我想在我的vb.net表格
中从网站提取字体标记之间的数字<html>
....
When asked enter the code: <font color=blue>24006 </font>
....
</html>
号码是自动生成的
我用:
Dim str As String = New WebClient().DownloadString(("http://www.example.com"))
Dim pattern = "When asked enter the code: <font color=blue>\d{5,}\s</font>"
Dim r = New Regex(pattern, RegexOptions.IgnoreCase)
Dim m As Match = r.Match(str)
If m.Success Then
Label1.Text = "Code" + m.Groups(1).ToString()
m = m.NextMatch()
Else
Debug.Print("Failed")
End If
但得到输出:
代码
===========================
谢谢
抱歉英语不好......
答案 0 :(得分:0)
Dim matchCollection As MatchCollection = regex.Matches("When asked enter the code: <font color=blue>24006 </font>","<font color=.*?>(.*?)</font>",ReaderOptions.None)
For Each match As Match In matchCollection
If match.Groups.Count >0 then
Console.WriteLine(match.Groups(1).Value)
end if
Next
或有点linq
Dim matchCollection As MatchCollection = regex.Matches("When asked enter the code: <font color=blue>24006 </font>","<font color=.*?>(.*?)</font>",ReaderOptions.None)
For Each match As Match In From match1 As Match In matchCollection Where match1.Groups.Count >0
Console.WriteLine(match.Groups(1).Value)
Next
有关详细信息,请参阅VB.NET Regex.Match和VB.NET Regex.Matches
答案 1 :(得分:0)
你应该not use regex to parse HTML。
选项: