【问题标题】:Phone Number Extracting using RegEx And HtmlAgilityPack In Vb.net在 Vb.net 中使用 RegEx 和 HtmlAgilityPack 提取电话号码
【发布时间】:2014-09-14 11:44:35
【问题描述】:

我已经编写了整个代码来从网站中提取手机号码 但问题是它完美地提取数字但非常缓慢它还在提取时挂起我的表单,请帮助我让它运行得更快。

更高效地运行。

Imports HtmlAgilityPack
Imports System.Text.RegularExpressions
Public Class Extractor

Shared doc As New HtmlAgilityPack.HtmlDocument()

Public Shared Function ScrapLinks(TextBox1 As TextBox, ListBox1 As ListBox, lbllinks As Label)
    Dim hw As New HtmlWeb()
    Try
        doc = hw.Load(TextBox1.Text)
        doc.LoadHtml(doc.DocumentNode.SelectSingleNode("//*[@id='ad_list']").InnerHtml())

        For Each link As HtmlNode In doc.DocumentNode.SelectNodes("//a[@href]")

            Dim hrefValue As String = link.GetAttributeValue("href", String.Empty)

            If hrefValue.Contains("/detail/") Then
                If Not ListBox1.Items.Contains(hrefValue) Then
                    ListBox1.Items.Add(hrefValue)
                End If
            End If
        Next

    Catch ex As Exception
        MsgBox("Error " + ex.Message)

    End Try
    Return Nothing

End Function

Public Shared Function Scrapnums(lstbox As ListBox,lstnum As ListBox)
    Try

        Dim hw As New HtmlWeb()
        doc = hw.Load(lstbox.SelectedItem)

        Dim data = doc.DocumentNode.SelectSingleNode("//*[@class='det_ad f_left']").InnerText

        Dim m As Match = Regex.Match(data, "(\+92|0092)-?\d{3}-?\d{7}|\d{11}|\d{4}-\d{7}")


        If Not lstnum.Items.Contains(m.Value) Then

            lstnum.Items.Add(m.Value)

        End If

    Catch ex As Exception


    End Try
    Return Nothing

End Function

End Class

【问题讨论】:

  • 这个问题属于codereview.stackexchange.com
  • 我在codereview上问过问题 谢谢参考!
  • 我设法为您刚刚删除的问题创建了一个可能的正则表达式。
  • @pushpraj 很抱歉,但我感觉很糟糕,因为我的问题有些低分。
  • @pushpraj 请在此处发布您的正则表达式!请。

标签: vb.net html-agility-pack


【解决方案1】:

这是一个解析电话号码的正则表达式

正则表达式

((?(?=\+92|0092)(?:\+|00)92\d?(?:-\d{3}-?\d{7}|-?\d{9}|\d{10})|(?:\d{11}|\d{3}\s\d{3}\s\d{5,6}|\d{4}-\d{7})))

样本编号

+92-3113143446
+923-113143446
032 124 26003
923 072 776037
03154031162
+923218923116
0307-2796038
+92-343-2842120

结果

  • 匹配 1
    1. [0-14]+92-3113143446
  • 比赛 2
    1. [15-29]+923-113143446
  • 第 3 场比赛
    1. [30-43]032 124 26003
  • 第 4 场比赛
    1. [44-58]923 072 776037
  • 第 5 场比赛
    1. [59-70]03154031162
  • 第 6 场比赛
    1. [71-84]+923218923116
  • 比赛 7
    1. [85-97]0307-2796038
  • 第 8 场比赛
    1. [98-113]+92-343-2842120

演示

Online Demo

上面的正则表达式是基于假设的,它可能匹配比上面列出的更多的模式。因此可能需要根据需要对其进行改进。

【讨论】:

  • 谢谢兄弟帮我很多忙。
  • 很高兴它对您有所帮助。其次不要担心否决票,等待一段时间看看是否有答案。您可以随时选择删除问题,您将找回失去的声誉。最后但并非最不重要的一点,快乐的编码:)
猜你喜欢
  • 2013-09-21
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 2013-08-28
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 2011-03-22
相关资源
最近更新 更多