Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

Need code to remove invalid email addresses from string

I have a string that can contain from 1 to unlimited email addresses. They are separated by commas. So for example....

strEmails = "person1@email.com, person2@email.com, person3@email.com, person4@email.com"

I need to write a vb.net function that goes thru and ELIMINATES any invalid email addresses. So, for example...

strEmails = "person1@email.com, personINVALID1@, person2@email.com, person3@email.com, personINVALID2, person4@email.com"

The function would eliminate ALL invalid email addresses (personINVALID1@ and personINVALID2) and strEmails would now be equal to....

"person1@email.com, person2@email.com, person3@email.com, person4@email.com"

Can you please provide example code to do this? (Function input: a string; output: new string) Thanks.
0
RobertNZana
Asked:
RobertNZana
  • 3
  • 2
2 Solutions
 
Anurag AgarwalCommented:
Imports System.Text.RegularExpressions

Private Function ExtractEmailAddressesFromString(ByVal source As String) _
        As String()
    Dim mc As MatchCollection
    Dim i As Integer

    ' expression garnered from www.regexlib.com - thanks guys!

    mc = Regex.Matches(source, "([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})")
    Dim results(mc.Count - 1) As String
    For i = 0 To results.Length - 1
        results(i) = mc(i).Value
    Next

    Return results
End Function

Open in new window


manipulte the code according to your need refer
0
 
käµfm³d 👽Commented:
Untested, but I imagine you could do something like this:
Imports System.Text.RegularExpressions

...

Public Function SanitizeEmailList(ByVal line As String) As String
    Dim candidates() As String = line.Split(",")
    Dim temp As New List(Of String)

    For Each candidate As String in candidates
        If Regex.Match(candidate, "^[^@]+@[^@]+?\.[a-zA-Z]{2,6}$") Then
            temp.Add(candidate)
        End If
    Next

    Return String.Join(temp.ToArray(), ",")
End Function

Open in new window

0
 
RobertNZanaAuthor Commented:
anurag_onnet - How can I return a regular (non-array) string as the result?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
Use String.Join() as I demonstrate  : )

Of course, now that I tested it, it seems I had the arguments backwards!

Here is the appropriate code:
Public Function SanitizeEmailList(ByVal line As String) As String
    Dim candidates() As String = line.Split(",")
    Dim temp As New List(Of String)

    For Each candidate As String In candidates
        If Regex.IsMatch(candidate, "^[^@]+@[^@]+?\.[a-zA-Z]{2,6}$") Then
            temp.Add(candidate)
        End If
    Next

    Return String.Join(",", temp.ToArray())
End Function

Open in new window

0
 
RobertNZanaAuthor Commented:
Thanks. How can I return only those which FAIL the match reg ex?
0
 
käµfm³d 👽Commented:
Change line 6 to:

    If Not Regex.IsMatch(candidate, "^[^@]+@[^@]+?\.[a-zA-Z]{2,6}$") Then
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now