Re: Regex help
| From: | Robin Vickery | Date: | Tue, 02 Aug 2005 14:12:31 +0000 |
| Subject: | Re: Regex help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-219831@lists.php.net to get a copy of this message | ||
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