|  | Posted by Robin Vickery on 08/02/05 17:12 
On 8/2/05, Chris Boget <chris.boget@wild.net> wrote:> I'm trying to validate an email address and for the life of me I
 > cannot figure out why the following regex is not working:
 >
 >   $email = "f.lastname@company-di.co.uk";
 >   $regex =
 > "^([a-zA-Z0-9_\.\-\']+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-\']+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
 
 [a-zA-Z0-9\-\']
 
 You don't escape hyphens like that in a character class. Put it at the
 end of the list of characters like this:
 
 [a-zA-Z0-9'-]
 
 Then it'll match your test address.
 
 If you're trying to find out why a regexp isn't working, try
 simplifying your test cases until it works. For example:
 
 $email = "f.lastname@company-di.co.uk"; // doesn't work
 $email = "lastname@company-di.co.uk";  // doesn't work
 $email = "lastname@company-di.com";   // doesn't work
 $email = "lastname@company.com";       // works!
 
 I don't suppose this is the place for a rant about the futility of
 checking email addresses with a regexp?
 
 -robin
  Navigation: [Reply to this message] |