473,382 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 5564
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
1
by: Colin Reid | last post by:
Hey MS, here's an apparent problem in the base class library. I pulled the email validation pattern "^((*)*@(()+(*)*\.)+{2,9})" from http://regexlib.com. If I validate the email address...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
10
by: j0mbolar | last post by:
say you have char buf = "string1 string2 string3"; then you want to use sscanf to match "string3" and store it into another array. so: char buf = "string1 string2 string3"; char array;
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
1
by: DMG | last post by:
I have used Regular Expressions very little! I have a textbox that I want to limit the data entered to be a date in the format as such: 14-Feb-2004 Two numbers {dash} Three characters {dash}...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: Support Desk | last post by:
That’s it exactly..thx -----Original Message----- From: Reedick, Andrew Sent: Tuesday, June 03, 2008 9:26 AM To: Support Desk Subject: RE: regex help The regex will now skip anything with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.