VB - Get all email addresses from text or string

By , 4/8/2008
(1 ratings)
This function can filter all email addresses out of a string and save's them into a List(Of String)


<a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fdotnet-snippets.com%2fdns%2fget-all-email-addresses-from-text-or-string-SID589.aspx"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fdotnet-snippets.com%2fdns%2fget-all-email-addresses-from-text-or-string-SID589.aspx" border="0" alt="kick it on DotNetKicks.com" /></a>
Public Function GetAllEMailAddresses(ByVal Input As String) As List(Of String)
    Dim Results As New List(Of String)


    Dim MC As Text.RegularExpressions.MatchCollection = _
    System.Text.RegularExpressions.Regex.Matches(Input, _
    "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")

    For i As Integer = 0 To MC.Count - 1
        If Results.Contains(MC(i).Value) = False Then
            Results.Add(MC(i).Value)
        End If
    Next

    Return Results
End Function

Tagged with email, regex.

1 Comments

By wekempf, 9/22/2008
Regular expressions are notoriously bad for use in validating/searching e-mail addresses. This one in particular is extremely buggy. There is a supposedly valid regex for this, but the code is extremely scary. Best just not to do this.
 

Log in, to comment!