Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Opposite of regex.matches?

Posted on 2011-03-08
9
Medium Priority
?
609 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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