Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match all email addresses at a specific domain using regex?

Tags:

regex

I need help on finding a regex expression that will match email addresses of only a specific domain

As in any .*@testdomain.com

And also the opposite any thing other than .*@testdomain.com

like image 659
sasuke Avatar asked Mar 04 '11 08:03

sasuke


People also ask

What kind of regex to use to validate email address?

In python: You haven't tell us what kind of regex flavor you need however this example will fit most of them: Assuming Unix style where . is any character: .*\..*@gmail\.com I used follwing regex expression to validate the email address.

How to find email address of a specific domain?

Email Hunter is a very easy online service that lets you find out Email address of a particular domain. Go to the website, just enter the domain name of your desired company like ampercent.com to find email addresses available in that domain.

Why can't I match an email address within a string?

Matching an email address within a string is a hard task, because the specification defining it, the RFC2822 , is complex making it hard to implement as a regex. For more details why it is not a good idea to match an email with a regex, please refer to the antipattern example when not to use a regex: for matching emails .

How do you make a regex look like an email?

When you need to rapidly validate an entry to make sure it looks like an email, the best option is to keep it simple: That regex will check that the mail address is a non-space separated sequence of characters of length greater than one, followed by an @, followed by two sequences of non-spaces characters of length two or more separated by a . .


Video Answer


2 Answers

Ay

I propose an expression very simple:

^[A-Za-z0-9._%+-][email protected]$

and for the negative check:

^[A-Za-z0-9._%+-]+@(?!testdomain.com)[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$

I hope could work for you

like image 165
Marcello Faga Avatar answered Sep 29 '22 23:09

Marcello Faga


@Kushal I found an easy way to do this, but it was somewhat tricky, because I had to retrieve via ajax the domain from a global variable stored somewhere else.

Here is the following line of code inside a javascript validation method:

return new RegExp("^\\w+([-+.']\w+)*@"+getDomain.responseJSON.d+"$").test(value.trim());

I create a new RegExp on the spot and test the value (email input) to see if it matches the domain.

  • The string inside quotes is the regex that may contain any kind of name the user has.
  • The getDomain.responseJSON.d, is the dynamic global domain variable (in case I want it changed and did not want to interfere with the source code.) that was retrieved with a $.getJSON(); call to a WCF service.

If you want something more elaborate but have standard domains, then it can go like this:

return /^\w+([-+.']\w+)*@?(example1.com|example2.com)$/.test(value.trim());
like image 36
SoftDev30_15 Avatar answered Sep 29 '22 22:09

SoftDev30_15