Re: regular expression functions

From: Date: Sat, 02 Sep 2000 07:32:32 +0000
Subject: Re: regular expression functions
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-2385@lists.php.net to get a copy of this message
pigfai wrote: > Another thing is ereg("^.+@.+\\..+$", $email) can be used to validate > e-mail format. But how can I improve it to have the functionality: if the > email address contains two "@", it can also validate it as an invalid > email address? Dot (.) will match any character within a regular expression. So, your regex will match two or more @ symbols Here is a slightly more restrictive regular expression that will not allow 2 @ symbols (Warning - not tested :) "^[^@]+@[^@]+(\.[^@]{2,}){1,2}$" Roughly translated, this regex means: ^: Starting a the beginning of the string [^@]+: match one or more of any character *except* @ @: match a literal @ symbol [^@]+: match one or more of any character *except* @ (\.[^@]{2,}): match a literal . followed by 2 or more of any character except @ {1,2}: let the previous match, be matched once or twice $: this should be the end of the string This is not the best way to validate email addresses - however, it will help you get a better grip in regexes. Drop by www.zend.com and check out their code library to see some email validation scripts. - Zak

« previous php.windows (#2385) next »