Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need code to remove invalid email addresses from string

Posted on 2011-03-08
6
Medium Priority
?
444 Views
Last Modified: 2012-06-27
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
Comment
Question by:RobertNZana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
Anurag Agarwal earned 1000 total points
ID: 35068349
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 35068408
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
 
LVL 9

Author Comment

by:RobertNZana
ID: 35069975
anurag_onnet - How can I return a regular (non-array) string as the result?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35070868
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
 
LVL 9

Author Comment

by:RobertNZana
ID: 35070886
Thanks. How can I return only those which FAIL the match reg ex?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35071151
Change line 6 to:

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question