Solved

Opposite of regex.matches?

Posted on 2011-03-08
9
577 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
 
LVL 9

Author Comment

by:RobertNZana
ID: 35071545
For some reason this email address is passing when it should not: myemail.@mail.net
0
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.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

708 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

11 Experts available now in Live!

Get 1:1 Help Now