Solved

Need code to remove invalid email addresses from string

Posted on 2011-03-08
6
437 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
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 250 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net Fit image buttons according to size of the screen 2 20
vb.net help 4 29
Store results in vb.net 3 20
VB.Net - Get ColumnNames of SQL Table 4 12
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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