472,145 Members | 1,509 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Regex for checking Email format?

I'm trying to design a regular expression that matches (using
preg_match()) when a string is a well-formed Email address.

So far I have this: /^[A-Z0-9._%\-]+@[A-Z0-9._%\-]+\.[A-Z]{2,4}$/i
I got that from reguar-expressions.info. But PHP keeps complaining of
"Unknown modifier 'Z'".

For the life of me I can't figure out how 'Z' is a modifier.
Can anyone help? Thanks much.

Jul 17 '05 #1
8 5476
un*****@gmail.com wrote:
I'm trying to design a regular expression that matches (using
preg_match()) when a string is a well-formed Email address.

So far I have this: /^[A-Z0-9._%\-]+@[A-Z0-9._%\-]+\.[A-Z]{2,4}$/i
I got that from reguar-expressions.info. But PHP keeps complaining of
"Unknown modifier 'Z'".

For the life of me I can't figure out how 'Z' is a modifier.
Can anyone help? Thanks much.


My PHP does not complain.

php$ php --version
PHP 4.3.9-1 (cli) (built: Oct 5 2004 08:45:32)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

php$ cat preg.php
<?php

$regexp = '/^[A-Z0-9._%\-]+@[A-Z0-9._%\-]+\.[A-Z]{2,4}$/i';
$email = 'h*****@dodgeit.com';

if (preg_match($regexp, $email)) {
echo 'Match!';
}
echo "\n";

?>

php$ php preg.php
Match!

Also see
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

This is a Perl regexp, it should work with PHP unchanged (never tried it).
--
Mail sent to my "From:" address is publicly readable at http://www.dodgeit.com/
== ** ## !! !! ## ** ==
TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #2
.oO(un*****@gmail.com)
I'm trying to design a regular expression that matches (using
preg_match()) when a string is a well-formed Email address.

So far I have this: /^[A-Z0-9._%\-]+@[A-Z0-9._%\-]+\.[A-Z]{2,4}$/i
I got that from reguar-expressions.info. But PHP keeps complaining of
"Unknown modifier 'Z'".


The regex works here, but there's a more general problem.

The syntax of an e-mail address can be rather complex, there are some
different formats described in RFC 822. It's no easy task (if possible
at all) to check a mail address for validity just with simple regular
expressions.

The regex above for example still allows invalid addresses (like
foo@1..bar), while keeping valid ones out (like fo**@example.com,
fo*@example.museum).

You might want to check out <http://www.phpclasses.org/emailvalidation>,
but even this class is not perfect.

Micha
Jul 17 '05 #3
W47
Pedro Graca wrote:
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html


Grand as it may be, that pattern does not cater for comments
in addresses.* The module can however replace any with
whitespace before the pattern is applied. Anyway, as its
description says, 'the only sure way to see if a supplied
email address is genuine is to send an email to it and see
if the user recieves [sic] it'.
* See my From header. Though Gravity2.60 allows me to put
comments in my address, it doesn't cope well: entering a
display name and an address with comments results in a
malformed From header.

Cheers!

--
Jock
Jul 17 '05 #4
Thank you very much. For some reason your logic worked, and mind
didn't. I still can't figure out why, because I have the exact same
regex and I had nearly identical logic.
Meh, no matter. It works now. Thanks again.

Jul 17 '05 #5
Thanks for the heads up... but this script is really a temporary thing.
I'm not aiming for bulletproof email validation, just something that is
good enough to keep the obviously-bogus addresses out. For applications
where I need bulletproof email validation, I usually send a
confirmation message.

Jul 17 '05 #6
un*****@gmail.com wrote:
I'm not aiming for bulletproof email validation, just something that is
good enough to keep the obviously-bogus addresses out.
So why not just check for the presence of a local part, a
'@' and a host name? Or even just an '@'? As Michael has
said, your pattern already allows obviously-bogus addresses
while disallowing perfectly reasonable ones.
For applications where I need bulletproof email validation, I usually
send a confirmation message.


Good.

--
Jock
Jul 17 '05 #7
OK, now I'm totally confused. I guess the best way to do it would be to
check for an '@' and try to determine if the hostname resolves to a
server...

Jul 17 '05 #8
In article <11**********************@f14g2000cwb.googlegroups .com>, un*****@gmail.com wrote:
OK, now I'm totally confused. I guess the best way to do it would be to
check for an '@' and try to determine if the hostname resolves to a
server...


And what if there are 2 @ in the address?
http://www.ex-parrot.com/%7Epdw/Mail...2-Address.html

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Colin Reid | last post: by
10 posts views Thread by j0mbolar | last post: by
9 posts views Thread by Tim Conner | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by Support Desk | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.