1

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?

martnu
  • 773
  • 4
  • 11

3 Answers3

2

([^@]*\.)? 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.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
0

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|)$~

rdamborsky
  • 1,920
  • 1
  • 16
  • 21
0

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.

unholysampler
  • 17,141
  • 7
  • 47
  • 64