Ultimate Java Regular Expression to validate any email address.
Feb 01
Java Java, RegEx, Regular Expression 37 Comments
My post about Java regular expression gets a lot of hits daily. Someone commented that the regular expression I included in that post does not block certain invalid email addresses. So I updated the Java regular expression to validate email address. I am pretty sure that the following Java regular expression will validate any email address.
"^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
Here is an example Java program to test this regular expression for email address validation.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidateEmailAddress{
public boolean isValidEmailAddress(String emailAddress){
String expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = emailAddress;
Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}
public static void main(String args[]){
ValidateEmailAddress vea = new ValidateEmailAddress();
String emailAddress = "[email protected]";
if(vea.isValidEmailAddress(emailAddress)){
System.out.println(emailAddress + " is a valid email address.");
}else{
System.out.println(emailAddress + " is an invalid email address.");
}
}
}
Give this regular expression a try. I am confident that no invalid email can get through this regular expression.
Enjoy.
If you enjoyed this post, make sure you subscribe to my RSS feed!






Feb 24, 2009 @ 11:06:59
“^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$”
Above does not seem to work with a valid [email protected]
So make ‘-word’ and ‘.word’ optional to allow [email protected]
“^[\\w\\-]?([\\.\\w]?)+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$”
Not sure if this is the correct corection to the regex. Might break catching some invalid email format. But it does allow the easy normal valid email syntax of [email protected] .
is ‘[email protected]’ even valid?? Both regex’s will take it as valid.
Apr 15, 2009 @ 09:49:36
I follow your posts for a long time and must tell you that your articles always prove to be of a high value and quality for readers.
Apr 16, 2009 @ 21:42:11
Thanks for your kind words. I am glad you find my post useful.
Apr 22, 2009 @ 07:45:31
That is what I was looking for. It worked liked a charm. Thanks
Apr 27, 2009 @ 12:54:10
WARNING: “^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$” Does not work!
- Only supports a hyphen at the begining of the address:
> “[email protected]” works
> “[email protected]” does not
- It allows underscore in the domain name which is not allowed.
The following will work better:
“^[\\w\\-]+(\\.[\\w\\-]+)*@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$”
Please update your post so that applications are not developed using the incorrect expression (considering this is among the top Google hits).
Jul 09, 2009 @ 05:26:52
The last expression also matches an e-mail like :
“[email protected]″ …
Jul 22, 2009 @ 15:48:39
Try using EmailValidator from commons-validator package. It works like a charm. Following is the link to it’s javadoc
http://commons.apache.org/validator/apidocs/org/apache/commons/validator/routines/EmailValidator.html
Oct 01, 2009 @ 08:39:35
How about
^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$
There are however more extensions.. just ad them yourself. Country codes are all automatically valid
Oct 27, 2009 @ 14:29:25
And even if you did get all 280, that would slow it down since it would have to check 280 TLDs before knowing it’s invalid.
Dec 01, 2009 @ 05:40:42
to allow for other domains, use
“^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$”;
Difference is only that it accepts lower-case letters at the end
Dec 24, 2009 @ 08:10:13
This is very helpful
thanks
Jan 01, 2010 @ 14:46:21
I’ve found some time about the best regexp for validating emails but it is for JavaScript and not java itself. If someone would be so nice to “convert” it so it would be syntax correct for java then we all would appreciate it verry much.
And the actual expression is as follows (Update 7):
/^((“[\w-\s]+”)|([\w-]+(?:\.[\w-]+)*)|(“[\w-\s]+”)([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
string result
[email protected] pass
[email protected] pass
[email protected] pass
[email protected] pass
[email protected] pass
[email protected] pass
[email protected] pass
guy.joel@[127.0.0.1] pass
\”Guy Joel\”guy.joel@[127.0.0.1 pass
\"Guy Joel\"@[127.0.0.1] pass
\”Guy Joel\”@9.999.99.25 fail
\”Guy Joel\”@999.99.99.25 fail
“\”Guy Joel\”" fail
As i said. It’s in javascript format and it cannot be used in it’s orginal form in java String.matches(regexp) because this way you cannot use \w and \s
Or if you find another way to use this regexp in java in its orginal form then please pose the answer.
Since it 1.01.2010 then i’d like to wish you all a happy new year.
Apr 15, 2010 @ 03:50:08
hugmin42 is right for Java swing, but i edit it a little.
I found out there was missing “” in the first and second “.”, so i changed it to “\.” and it works really good!
^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|nl|be|de|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$
GL & HF
Jul 17, 2010 @ 12:36:12
Can I use your code in my project?
Jul 19, 2010 @ 08:32:04
You sure can.
Sep 13, 2010 @ 10:37:48
Try the following string. It is not working
sdfsdafds
Oct 03, 2010 @ 20:06:53
Multiple dots pass validation (e.g. user@domain….com)
Oct 26, 2010 @ 09:36:12
Great one! Saved me time doing it myself.
@Mike I don’t think multiple dots pass the validation, unless Zaheer updated the regex
Tulio Domingos
Nov 16, 2010 @ 10:23:17
I can see just by looking at it that it doesn’t allow the full range of dot-atom characters, a quoted string local-part, domain literals, internationalized labels, .museum, or .travel.
Try this:
‘/^((?>(?>(?>((?>[ ]+(?>\x0D\x0A[ ]+)*)?)(\((?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)([!#-\'*+\/-9=?^-~-]+|”(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*(?2)”)(?>(?1)\.(?1)(?4))*(?1)@(?1)(?>((?>xn--)?[a-z0-9]+(?>-[a-z0-9]+)*)(?>(?1)\.(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD’
Aug 22, 2011 @ 22:33:07
That was awesome!! Helped me in my assignment!!
Sep 07, 2011 @ 08:20:20
[email protected] = fail
Fell at the first hurdle
Oct 06, 2011 @ 17:23:47
The first example in this post fails with the example
[email protected]
Nov 01, 2011 @ 07:10:43
how to validate email address like [email protected] i.e it doesnot allow
Nov 16, 2011 @ 10:54:48
Hi. E-mails with only 2 characters in the beginning are failing.
[email protected] = fail
[email protected] = fail
Dec 02, 2011 @ 03:43:03
exactly what I’m looking for.
Dec 05, 2011 @ 09:03:25
schön@grün.de fails – \w doesn’t match “umlaute” in java..
Dec 08, 2011 @ 14:15:26
The reason this doesn’t allow any email addresses because it’s looking for CAPITAL LETTERS on the domain name.
CURRENT : ^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$”;
SHOULD BE: ^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4}$”;
OR
SHOULD BE: ^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-z]{2,4}$”;
Dec 08, 2011 @ 14:15:52
The reason this doesn’t allow any email addresses because it’s looking for CAPITAL LETTERS on the domain name.
CURRENT : ^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$”;
SHOULD BE: ^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4}$”;
OR
SHOULD BE: ^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-z]{2,4}$”;
Dec 14, 2011 @ 16:54:12
From the Struts 2 email validator:
^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+((\\.com)|(\\.net)|(\\.org)|(\\.info)|(\\.edu)|(\\.mil)|(\\.gov)|(\\.biz)|(\\.ws)|(\\.us)|(\\.tv)|(\\.cc)|(\\.aero)|(\\.arpa)|(\\.coop)|(\\.int)|(\\.jobs)|(\\.museum)|(\\.name)|(\\.pro)|(\\.travel)|(\\.nato)|(\\..{2,3})|(\\..{2,3}\\..{2,3}))$
Dec 23, 2011 @ 05:45:53
Ultimate my ass. How can you enforce upper case letters for the domain part at the end ? ([\\w\\-]+\\.)+[A-Z]{2,4}$
Most of the commenters here are wrong as well.
Dec 23, 2011 @ 05:54:12
String EMAIL_PATTERN = “^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[a-z,A-Z]{2,})$”;
Feb 28, 2012 @ 04:32:28
How to Validate Email Address using Java Pattern
Validate Email Address using Java Pattern
Mar 06, 2012 @ 14:44:33
Doesn’t validate: [email protected]
Apr 17, 2012 @ 20:28:31
Thx for share.
Jun 28, 2012 @ 18:43:00
Update 2,000,000….
You CANNOT validate an email address using a REG expression.
READ and UNDERSTAND the RFC’S of what is allowed.
Then you might understand.
Feb 20, 2013 @ 10:05:18
This will work with any email(that I’ve seen), it accepts only “. _ -” before @.
“\\w+[\\._-]\\w+@\\w+\\.\\w{2,4}” +
“|\\w+[\\._-]\\w+@\\w+\\.\\w{2,4}.\\w{2,4}” +
“|\\w+@\\w+\\.\\w{2,4}” +
“|\\w+@\\w+\\.\\w{2,4}.\\w{2,4}”