[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

Regluar Expression

I have the following regular expression for email addresses that works pretty well.

^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$

How do I modify it to only allow email addresses within a specific sub domain

i.e.

email@mydomain.com - OK
email@test.mydomain.com - OK
email@test1.test2.test3.mydomain.com - OK
email@yahoo.com - BAD

So it must end with mydomain.com

Thanks.
0
mrichmon
Asked:
mrichmon
  • 8
  • 7
  • 5
  • +3
1 Solution
 
Jaime OlivaresCommented:
Why not to use a second regex?
0
 
mrichmonAuthor Commented:
Why when the above can be modified?

I am very close.  I now have :

^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.com)+$

Which only allows ones ending in .com

I also tried

^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(mydomain\.com)+$

Which recognizes all of the subdomains, but not the main domain....
0
 
Jaime OlivaresCommented:
something like:
^.+@.+mydomain\.com$
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrichmonAuthor Commented:
^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(mydomain\.com)+$ also has the problem of recognizing

test@testmydomain.com as OK when it should be BAD
0
 
mrichmonAuthor Commented:
Okay I think this does it :

^\w+([\.-]?\w+)*@(\w+[\.-])*(mydomain\.com)+$
0
 
ren_bCommented:
might want to change it to: ^\w+([\.-]?\w+)*@((\w+[\.-])+\w+\.)?mydomain\.com$
unless bob@mydomain.commydomain.commydomain.com or someone@blah-mydomain.com are valid
0
 
mrichmonAuthor Commented:
someone@blah-mydomain.com is valid

bob@mydomain.commydomain.commydomain.com   should not be valid
0
 
ren_bCommented:
then all you have to do is delete 3 char's from yours :)
^\w+([\.-]?\w+)*@(\w+[\.-])*mydomain\.com$
0
 
adg080898Commented:
If you really want to enforce "@mydomain.com":

^\w+([\.-]?\w+)*@mydomain\.com$

Is this perl? Watch out for the @ if it is. (Put a backslash before it)

Why worry so much about every character being a word and a period not being at the beginning or the end. Don't worry about the username so much. Your regex won't accept my email address, which contains digits. Why not this?:

^([^@: ]*)@mydomain\.com$

It enforces that the username does not contain spaces, or colons or @ characters and captures the username. Then it enforces that it is followed by the exact string "@mydomain.com" which must be followed by the end of the string.
You might want to discard and ignore leading and trailing spaces:

^\s*([^@: ]*)@mydomain\.com\s*$

If your program is not going to need to capture the username from the string, you can remove the "(" and ")".

^\s*[^@: ]*@mydomain\.com\s*$

0
 
adg080898Commented:
I just noticed that you wanted subdomains to work, so:

^\s*[^@: ]*@([^.]+(\.[^.]+)*\.mydomain.com|mydomain\.com)\s*$

Which language are you using? You may need "ungreedyness".
0
 
ren_bCommented:
adg: yours will match ``i'm*~*preciou$,*~*&*~*i*~*love*~*every*~*boy@mydomain.com'', or even just ``@mydomain.com'' which is of no use, unless he's just searching a list of company emails.
0
 
ozoCommented:
mrichmon, fred&barney@stonehenge.com is a perfectly valid email address which your regular expression would not match
0
 
ren_bCommented:
this will match all valid email addresses... :)

(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(
?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[
\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\0
31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\
](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+
(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:
(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)
?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\
r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[
 \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)
?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]
)*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[
 \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*
)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)
*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+
|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r
\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^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?:\r\n)?[ \t])*(?
:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[
\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-
\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(
?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;
:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([
^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"
.\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\
]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\
[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\
r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\]
\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]
|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \0
00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\
.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,
;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|"(?
:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*
(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".
\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[
^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\]
]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(
?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(
?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[
\["()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t
])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t
])+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?
:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|
\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:
[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".\[\
]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)
?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["
()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)
?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>
@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[
 \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,
;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]
)*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\
".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?
(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:\\".
\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:
\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[
"()<>@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])
*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])
+|\Z|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\
.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\["()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(
?:\r\n)?[ \t])*))*)?;\s*)

it may be a valid email, but it may not fit his context.
0
 
adg080898Commented:
To everybody:
Remember, the asker wants to match addresses ****ending in mydomain.com****, NOT any valid email address!!

To ren_b:

Yes, my regexp would match that ridiculous address. It is easy to add restrictions though, just add more characters to the first character class. The question is, EXACTLY which characters are not allowed? Just because the characters look invalid, doesn't mean they are. The RFC 822 specifications say to pass usernames through unvalidated (unless you can point out something in there that says something different).

You do have a very good point with the "@mydomain.com" comment. That's easy to fix, just change the "*" to a "+":

^\s*[^@: ]+@([^.]+(\.[^.]+)*\.mydomain.com|mydomain\.com)\s*$
0
 
kolpdcCommented:
^\w+([\.-]?\w+)*@(?:\w*\.)*mydomain(\.\w{2,3})+$
did not read the whole thread, but think, this should work. try to get the little tool expresso from http://www.ultrapico.com - very helpful
0
 
kolpdcCommented:
sorry, pasted wrong expression.
right one also matching domain is ^\w+([\.-]?\w+)*@(?:\w*\.)*mydomain\.com$
0
 
mrichmonAuthor Commented:
Wow  a lot of comments over the weekend.

Let me try to address some of them.

So far I like ren_b's solution of ^\w+([\.-]?\w+)*@(\w+[\.-])*mydomain\.com$

adg is correct in that I do not want any valid email address - just those ending in mydomain.com
adg is incorrect in that my regex WILL match email expressions with digits - he/she said it would not

ozo : fred&barney@stonehenge.com is not valid in our domain so I don't need to worry about that.  But I have never seen a site that allows an & as part of the email address.  It is considered invalid email address everywhere I have ever seen.  Can you point out somewhere that accepts this email address?  And by somewhere - I mean a major site.

kolpdc - what is the point of the ?: that you added


One general question - what does the + mean in the regular expression.

Thanks for all the comments.
0
 
ren_bCommented:
+ means one or more {1,}
0
 
kolpdcCommented:
+ one or more
? zero or one

do you mean after @? its a non-capturing group "(?:)". takes all alphanumerics + "." (the subdomains).
0
 
mrichmonAuthor Commented:
Ah tahnks.  I knew about ? and * but now + makes sense too.

However, I am still confused about the ?:

What would

^\w+([\.-]?\w+)*@(?:\w*\.)*mydomain\.com$

catch that

^\w+([\.-]?\w+)*@(\w+[\.-])*mydomain\.com$

would not?


0
 
adg080898Commented:
I think ?: means "don't capture". Don't put it in $1 or $2 etc...
0
 
adg080898Commented:
Are you using Perl? Regular expressions do vary in dialect.
0
 
Jaime OlivaresCommented:
maybe my suggestion of using 2 regex is still valid.
0
 
ren_bCommented:
well, ^\w+([\.-]?\w+)*@(?:\w*\.)*mydomain\.com$ would match blah.lbah@.mydomain.com whereas the second wouldn't match that as valid. but it would only put .lbah into $1. and it generally doesn't matter if you use ?: if you're not capturing parts out of a string.
0
 
mrichmonAuthor Commented:
I am not using perl.

I am writing a regex that can be used accross multiple languages.  Right now the one I have works accross ASP, ASP.NET, Javascript, Cold Fusion
0
 
ren_bCommented:
javascript and asp should be fine perl regex's, not sure about cold fusion
0
 
mrichmonAuthor Commented:
I know that all of the afroemetnioned ones can accept the same regular expressions.

0
 
kolpdcCommented:
no problem. i think all of the ones you mentioned will interprete regex the same way. does not matter if you are using perl, php or something of .net
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 7
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now