开发者

Regular expression for e-mail domain (not basic e-mail verification)

I'开发者_开发百科m currently using

if(preg_match('~@(semo\.edu|uni\.uu\.se|)$~', $email)) 

as a domain check.

However I need to only check if the e-mail ends with the domains above. So for instance, all these need to be accepted:

hello@semo.edu
hello@student.semo.edu
hello@cool.teachers.semo.edu

So I'm guessing I need something after the @ but before the ( which is something like "any random string or empty string". Any regexp-ninjas out there who can help me?


([^@]*\.)? works if you already know you're dealing with a valid email address. Explanation: it's either empty, or anything that ends with a period but does not contain an ampersand. So student.cs.semo.edu matches, as does plain semo.edu, but not me@notreallysemo.edu. So:

~@([^@]*\.)?(semo\.edu|uni\.uu\.se)$~

Note that I've removed the last | from your original regex.


You can use [a-zA-Z0-9\.]* to match none or more characters (letters, numbers or dot):

~@[a-zA-Z0-9\.]*(semo\.edu|uni\.uu\.se|)$~


Well .* will match anything. But you don't actually want that. There are a number of characters that are invalid in a domain name (ex. a space). Instead you want something more like this:

[\w.]*

I might not have all of the allowed characters, but that will get you [A-Za-z0-9_.]. The idea is that you make a list of all the allowed characters in the square brakets and then use * to say none or more of them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜