Visual Basic Code Snippet - Extract URLs

Visual Basic Code Snippet - Extract URLs

Visual Basic Code Snippet - Extract URLs

(VB) Visual Basic code snippet to extracts all the URLs from a string. ExtractURLs returns string array of URLs successful matches by iteratively applying a regular expression pattern to the input string.

Bookmark:

Visual Basic Code Snippet - Extract URLs

This .Net Visual Basic (VB) code snippet extracts all the URLs from a string. Data mining for URLs done by set of successful matches found by iteratively applying a regular expression pattern to the input string.

Public Function ExtractURLs(ByVal str As String) As String()
    ' match.Groups["name"].Value - URL Name
    ' match.Groups["url"].Value - URI
    Dim RegexPattern As String = "<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>"

    ' Find matches.
    Dim matches As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(str, RegexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)

    Dim MatchList(matches.Count - 1) As String

    ' Report on each match.
    Dim c As Integer = 0
    For Each match As System.Text.RegularExpressions.Match In matches
        MatchList(c) = match.Groups("url").Value
        c += 1
    Next match

    Return MatchList
End Function


VB Keywords Used:

  • Regex
  • Match
  • MatchCollection
  • RegexOptions.IgnoreCase
  • Regex.Matches

Code Snippet Information:

  • Applies To: .Net, VB, Visual Basic, CLI, Data Mining, URL Extract, Regular Expression
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment