473,763 Members | 5,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.inf o. 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 5584
un*****@gmail.c om 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.inf o. 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($re gexp, $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*****@gma il.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.inf o. 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.co m,
fo*@example.mus eum).

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.c om 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************ **********@f14g 2000cwb.googleg roups.com>, un*****@gmail.c om 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
4668
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 functions are simplified versions of the full featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form UNQUOTE
1
1727
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 "test@someverylongemailaddress.com" against it by just creating a RegEx and calling IsMatch it works fine, but if I create a schema defining a simple type restricting an xs:string by the regex pattern, it takes over full minute at 100% cpu to match....
4
9768
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 numeric value according to an arbitrary regular expression.
10
7616
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
4588
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
1039
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} Four numbers I don't care about checking for validity. I have tried the RegEx Library website but can't find anything in this format.
11
3111
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 a hand? import regsub
15
50258
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
1630
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 an '@'in the filename on the assumption it's already in the correct format. Uncomment the os.rename line
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.