Page 1 of 1

Regex for getting email

Posted: Wed Feb 17, 2010 4:14 am
by klevis miho
I have the regular expression for stripping emails from a given text:

"/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/i"

What I want is to get also the emails with an invalid syntax.

Basically I want users to enter their emails on a textbox, and to perform actions on the ones with a valid syntax, and to display error messagse to the users for an invalid email syntax for the invalid emails.


Will appreciate any tip.

Re: Regex for getting email

Posted: Wed Feb 17, 2010 11:59 am
by ridgerunner
The regex you posted already matches invalid emails! It has some interesting features...

The sub-expression '([ ]+|)' is used twice, (immediately before and after the '@' at sign). This is saying: "match one or more spaces or match nothing at all". (This expression is equivalent to the simpler '(\x20*)'. But why would you accept spaces around the '@' sign? This is invalid!

Also, the first and last capturing groups use the sub-expression: '([\s]*)'. There is no need to use a character class when you are matching a single char. It is better to write this simply as: '(\s*)'.

The premise of your question has me baffled. It is very easy to write a regex to match text that is not a valid email. You could use: '\S+'.

???!

Re: Regex for getting email

Posted: Wed Feb 17, 2010 3:37 pm
by AbraCadaver
If you are validating an email, just use:

Code: Select all

filter_var($variable, FILTER_VALIDATE_EMAIL);

Re: Regex for getting email

Posted: Wed Feb 17, 2010 3:43 pm
by superdezign
AbraCadaver wrote:

Code: Select all

filter_var($variable, FILTER_VALIDATE_EMAIL);
Nice little gem.

Sadly, looking at the comments on php.net, this function is based purely on the RFC specification, which is not the same thing as the "practical" specification, such as having the requirement of a TLD in the domain. Still, I did not know that this existed until just now.

^_^

Re: Regex for getting email

Posted: Wed Feb 17, 2010 4:02 pm
by AbraCadaver
superdezign wrote:
AbraCadaver wrote:

Code: Select all

filter_var($variable, FILTER_VALIDATE_EMAIL);
Nice little gem.

Sadly, looking at the comments on php.net, this function is based purely on the RFC specification, which is not the same thing as the "practical" specification, such as having the requirement of a TLD in the domain. Still, I did not know that this existed until just now.

^_^
I had never seen that comment and I don't know if it changed in some version, but it has always worked for me:

Code: Select all

var_dump(filter_var('shawn', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('shawn@localhost', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('shawn@example.com', FILTER_VALIDATE_EMAIL));

Code: Select all

bool(false)
bool(false)
string(17) "shawn@example.com"