Visual Basic Code Snippet - Extract Emails

Visual Basic Code Snippet - Extract Emails

Visual Basic Code Snippet - Extract Emails

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

Bookmark:

Visual Basic Code Snippet - Extract Emails

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

Public Function ExtractEmails(ByVal str As String) As String()
    Dim RegexPattern As String = "\b[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}\b"

    ' 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.ToString()
        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, Email Extract, Regular Expression
  • Programming Language : Visual Basic (VB)

External Resources:

Leave a comment