Code: Select all
/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/iMy regex abilities are very limited so I was wondering if someone could give me a bit of help or offer a better regex to check against.
Moderator: General Moderators
Code: Select all
/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/iCode: Select all
"/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"Code: Select all
"/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9]+[a-z0-9\-]*\.)+[a-z]{2,6}$/ix"Code: Select all
"/^([a-z0-9]+[a-z0-9\+_\-]*)(\.[a-z0-9\+_\-]+)*@([a-z0-9]+[a-z0-9\-]+\.)+[a-z]{2,6}$/ix"Code: Select all
if (preg_match('/^([a-z0-9]+[a-z0-9\+_\-]*)(\.[a-z0-9\+_\-]+)*@([a-z0-9]+[a-z0-9\-]+\.)+[a-z]{2,6}$/ix', '-scott_t_@-hotmail.com'))
{
die('good');
}
else
{
die('bad');
}
did you mean "can't"?Although, I can see now they do not validate the domain name right (it can start with "-").
There are a couple of things wrong with the regex:shiznatix wrote:I have been using the same email validation regex for forever and havn't had any problems...until now. The regex I have been using is this:but for whatever reason it won't match scott_t_@hotmail.comCode: Select all
/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i
My regex abilities are very limited so I was wondering if someone could give me a bit of help or offer a better regex to check against.
Code: Select all
'/^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,}$/i'Code: Select all
'/^[a-z0-9]+([-_.a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,}$/i'There is no need to escape the + and in this case the - inside the character class.VladSun wrote:CI uses this in its Validation class:...Code: Select all
"/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"
Code: Select all
[-a-c] // matches '-', 'a', 'b' or 'c'
[ABC-] // matches 'A', 'B', 'C' or '-'
[^a] // matches anything except 'a'
[a^+] // matches 'a', '^' or '+'Ops...shiznatix wrote:did you mean "can't"?Although, I can see now they do not validate the domain name right (it can start with "-").
It's so for sureprometheuzz wrote:There is no need to escape the + and in this case the - inside the character class.VladSun wrote:CI uses this in its Validation class:...Code: Select all
"/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"
Well, I've never worked with that framework, so I wouldn't know what to say to the developers exactly. I mean, I don't know where they've written that regex.VladSun wrote:It's so for sureprometheuzz wrote:There is no need to escape the + and in this case the - inside the character class.VladSun wrote:CI uses this in its Validation class:...Code: Select all
"/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"
You may write it to: http://codeigniter.com
A loose match:shiznatix wrote:Ok so all together now, what would be the one regex for this to rule them all? I don't mind if some non-ok emails get through so it can be a bit loose.
Code: Select all
#!/usr/bin/php
<?php
// http://www.regular-expressions.info/email.html
$address = 'scott_t_@hotmail.com';
if(preg_match('/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i', $address)) {
print "$address is valid\n";
} else {
print "$address is not valid\n";
}
?>Why not make the first part possessive? The character class cannot match "@", so there is no point in backtracking all the way to the beginning to recheck for an "@".prometheuzz wrote:Code: Select all
/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i
Code: Select all
/^[a-z0-9._%+-]++@[a-z0-9.-]+\.[a-z]{2,4}$/iCode: Select all
/^(?!\.)[a-z0-9._%+-]++(?<!\.)@[a-z0-9.-]+\.[a-z]{2,4}$/iTwo reasons:GeertDD wrote:Why not make the first part possessive? The character class cannot match "@", so there is no point in backtracking all the way to the beginning to recheck for an "@".prometheuzz wrote:Code: Select all
/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i