Solved

Opposite of regex.matches?

Posted on 2011-03-08
9
578 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

21 Experts available now in Live!

Get 1:1 Help Now