Solved

Regex not recognising correct emails

Posted on 2011-02-11
8
414 Views
Last Modified: 2012-05-11
I am trying to use RegEx to check email address's and do an action when they dont match

I have tried the code below but this seems to not match everything, i have tested this by removing the extention from the end of an email and a normal .co.uk extention on a second email both emails fail the test..

Can anyone help with this please
Set RegularExpressionObject = New RegExp

		With RegularExpressionObject
		.Pattern = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"
		.IgnoreCase = True
		.Global = True
		End With

		Set expressionmatch = RegularExpressionObject.Execute(" & Me.Recipient & ")

		If expressionmatch.Count = 0 Then

Open in new window

0
Comment
Question by:QuinnDester
8 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 34871125
Please try this pattern: "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34871280
Can you provide sample of addresses which do not match?
0
 
LVL 84

Expert Comment

by:ozo
ID: 34871327
One example of a valid email address that does not match is
fred&barney@stonehenge.com
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 3

Author Comment

by:QuinnDester
ID: 34871376
I have fixed this thanks the regex does match where it should it wasnt working because i used " & &" to include the variable (Silly Mistake)

but when we have an account closed we add .clossed to the end of the email address, how do i add to the regex to exclude any address that ends in .closed

Thanks
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 34871428
@ozo:

Yes, there will be many it doesnt match... but then the standards set down for emails include so many different chars, our site is aimed at UK users so standard matching is enough, and this regex is just to filter out who the email is sent to, it wont stop people using the site or signing up, so i am not to fussy about how precise it is..
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34871656
@ozo

I'm beginning to think you have a key on your keyboard bound to the string "fred&barney@stonehenge.com" for questions like these   = )


>>  how do i add to the regex to exclude any address that ends in .closed

Try this mod out:
.Pattern = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}(?!\.closed)(?!\.[A-Z0-9.-]+)\b"

Open in new window

0
 
LVL 3

Author Comment

by:QuinnDester
ID: 34871746
works perfectly... thank you
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34871891
NP. Glad to help  : )
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 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