Solved

Opposite of regex.matches?

Posted on 2011-03-08
9
582 Views
Last Modified: 2012-05-11
Right now I'm using reg expressions to locate only valid email addresses within a long string. This string is a bunch of email addresses separated by a comma.

mc = Regex.Matches(strEmailAddresses, "([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})")
               
How can I find out only the ones that do NOT match? ie that FAIL the regular expression?
0
Comment
Question by:RobertNZana
  • 4
  • 4
9 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 35070857
How is your string structured - ie how are email addresses split up within the string?
0
 
LVL 9

Author Comment

by:RobertNZana
ID: 35070875
Simple string. All email addresses separated by comma. Purpose of this function is to report back those email addresses which are invalid.

Example: "myaddress1@mail.com, myaddress2@mail.com, INVALID_EMAIL@, myaddress3@mail.com"

The function should return "INVALID_EMAIL@".
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35071067
Try this:

Dim strEmailAddresses As String = "myaddress1@mail.com, myaddress2@mail.com, INVALID_EMAIL@, myaddress3@mail.com"
Dim emailAddr As String

For Each emailAddr in strEmailAddresses.Split(",")
	emailAddr = emailAddr.Trim()
	If Not Regex.IsMatch(emailAddr, "([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})") Then
		Console.WriteLine("'{0}' invalid email address", emailAddr)
	End If
Next

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 9

Author Comment

by:RobertNZana
ID: 35071545
For some reason this email address is passing when it should not: myemail.@mail.net
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35071931
Please change the regular expression to:

    ^[a-zA-Z0-9_\-]+([_\-\.]?[a-zA-Z0-9]+)*?@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$

This one better handles the dot, so the following email addresses are not valid:

    my.email.@address.com
    .my.email@address.com
    my..email@address.com

Dim strEmailAddresses As String = "my.email.@address.com, .my.email@address.com, my..email@address.com, myaddress1@mail.com, myaddress2@mail.com, INVALID_EMAIL@, myaddress3@mail.com, a@address.com"
Dim emailAddr As String

For Each emailAddr in strEmailAddresses.Split(",")
        emailAddr = emailAddr.Trim()
        If Not Regex.IsMatch(emailAddr, "^[a-zA-Z0-9_\-]+([_\-\.]?[a-zA-Z0-9]+)*?@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$") Then
                Console.WriteLine("'{0}' invalid email address", emailAddr)
        End If
Next

Open in new window

0
 
LVL 9

Author Comment

by:RobertNZana
ID: 35071957
But will john.smith@att.net work? This is valid.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35072301
Yes, that works.  I mean is a valid email, so Regex.IsMatch returns true.
0
 
LVL 9

Author Comment

by:RobertNZana
ID: 35073562
Thanks. What is the reg ex statement to check if an email is valid?
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35074210
Regex.IsMatch just returns true if the string provided matches the regular expression.  In this case the regular expression is to validate the email address format.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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