Solved

Opposite of regex.matches?

Posted on 2011-03-08
9
597 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

628 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