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.
Regex for getting email
Moderator: General Moderators
-
klevis miho
- Forum Contributor
- Posts: 413
- Joined: Wed Oct 29, 2008 2:59 pm
- Location: Albania
- Contact:
- ridgerunner
- Forum Contributor
- Posts: 214
- Joined: Sun Jul 05, 2009 10:39 pm
- Location: SLC, UT
Re: Regex for getting email
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+'.
???!
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+'.
???!
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Regex for getting email
If you are validating an email, just use:
Code: Select all
filter_var($variable, FILTER_VALIDATE_EMAIL);mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Regex for getting email
Nice little gem.AbraCadaver wrote:Code: Select all
filter_var($variable, FILTER_VALIDATE_EMAIL);
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.
^_^
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Regex for getting email
I had never seen that comment and I don't know if it changed in some version, but it has always worked for me:superdezign wrote:Nice little gem.AbraCadaver wrote:Code: Select all
filter_var($variable, FILTER_VALIDATE_EMAIL);
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.
^_^
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"mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.