【问题标题】:Regular Expressions (regex) in vb.netvb.net 中的正则表达式 (regex)
【发布时间】:2014-01-15 20:31:16
【问题描述】:

vb.net 2010 中的正则表达式

我想从我的 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

但是得到了输出:

代码

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

谢谢

抱歉英语不好...

【问题讨论】:

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


    【解决方案1】:

    这样的东西应该可以帮助你。异常处理由您决定。

    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

    【讨论】:

      【解决方案2】:

      你应该not use regex to parse HTML

      选项:

      1. HTML Agility Pack这样的解析器
      2. HTMLDocument.GetElementsByTagName 中暴露的解析器
      3. 任何其他 HTML 解析器

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-24
        • 1970-01-01
        相关资源
        最近更新 更多