2

I am struggling with writing a regex for validating email address for only one domain.

I have this expression

[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}

But the issue is that for example hello@gmail.com.net is valid and I only want to be only valid for only one domain. So hence I do not want hello@gmail.com.net to be valid.

Help is needed. Thank you!

Barmar
  • 741,623
  • 53
  • 500
  • 612
habed
  • 201
  • 4
  • 12
  • 2
    Please give more examples of what you want to match and what you want not to match. – Yunnosch May 02 '17 at 21:47
  • Welcome to StackOverflow. Please take the [tour], learn asking good questions stackoverflow.com/help/how-to-ask, make a [mcve]. An MCVE should include a variety of sample input (illustrating all aspects) and desired output. If your question involves regular expressions name the "flavor" (i.e. the program you are using). – Yunnosch May 02 '17 at 21:48
  • What do you mean by only one domain? You mean it should only work for `@gmail.com`, but not any other domain? Or do you mean domains with only one `.` in them? – Barmar May 02 '17 at 21:53
  • @Barmar Yes, I mean that it should work with other domains BUT with only one . (dot) in them. Sorry for the confusion. – habed May 02 '17 at 22:21
  • @Yunnosch It should work with all the domains, just that it must only include a . dot in them. Examples: h@g.com, h@h.net, h@g.global NOT h@g.com.global -> I hope you understand my point. – habed May 02 '17 at 22:22
  • You mean a single dot? In that case I recommend the answer by fastr.de. Please edit your question to add the information from your comment. – Yunnosch May 02 '17 at 22:25
  • @Yunnosch exactly, I mean a single dot. Yeah i looked over the answer of fastr.de, worked on it a bit and it works like a charm. The question is also edited. – habed May 02 '17 at 22:32
  • Any particular reason why you would want to do this? Lots of legitimate email addresses are in nested domains. For instance, most addresses in the UK are `@something.co.uk` or `@something.ac.uk`, etc. – Barmar May 03 '17 at 15:08

5 Answers5

3

try this [A-Z0-9a-z._%+-]+@[A-Za-z0-9-]+\.[A-Za-z]{2,64}. In your regex is a dot in the allowed characters behind the @.

fastr.de
  • 1,497
  • 14
  • 23
0

You can use something like:

\b[A-Z0-9a-z._%+-]+@gmail\.com\.net\b

enter image description here

Regex Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

I found this regex for Swift:

[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}

It has an extra backslash.

I found it here: http://emailregex.com/

Regards,

Melle

melle
  • 26
  • 5
0

I know you already accept an answer but this idea just cross my mind. You can use URLComponents to split the email address into user and host and validate each component separately:

func validate(emailAddress: String) -> Bool {
    guard let components = URLComponents(string: "mailto://" + emailAddress),
        let host = components.host else
    {
        return false
    }

    return host.components(separatedBy: ".").count == 2
}

print(validate(emailAddress: "hello@gmail.com"))        // true
print(validate(emailAddress: "hello@gmail.com.net"))    // false
print(validate(emailAddress: "hello"))                  // false

Your requirement has a big flaw in it though: valid domains can have two dots, like someone@bbc.co.uk. Getting a regex pattern to validate an email is hard. Gmail, for example, will direct all emails sent to jsmith+abc@gmail.com to the same inbox as jsmith@gmail.com. The best way is to perform some rudimentary check on the email address, then email the user and ask them to click a link to confirm the email.

Code Different
  • 90,614
  • 16
  • 144
  • 163
0

You can try with below pattern.

/^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@((REPLACE_THIS_WITH_EMAIL_DOMAIN+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;

for Eg.

/^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@((gmail+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;