Solved

Need code to remove invalid email addresses from string

Posted on 2011-03-08
6
435 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
  • 3
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
Anurag Agarwal earned 250 total points
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
Comment Utility
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
Comment Utility
anurag_onnet - How can I return a regular (non-array) string as the result?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Thanks. How can I return only those which FAIL the match reg ex?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Change line 6 to:

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now