vb.net中的正则表达式(正则表达式)

时间:2014-01-15 11:58:50

标签: regex vb.net vb.net-2010

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

但得到输出:

代码

===========================

谢谢

抱歉英语不好......

2 个答案:

答案 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.MatchVB.NET Regex.Matches

答案 1 :(得分:0)

你应该not use regex to parse HTML

选项:

  1. HTML Agility Pack
  2. 这样的解析器
  3. HTMLDocument.GetElementsByTagName
  4. 中公开的解析器
  5. 任何其他HTML解析器