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 = "ab.cd@xyz.com";
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.
Popularity: 37% [?]
If you enjoyed this post, make sure you subscribe to my RSS feed!Related posts:
- Validate email address using JavaScript regular expression
- How to validate date using Java regular expression
- How to validate email, SSN, phone number in Java using Regular expressions.
- How to search for a string in a file using Java regular expression.
- Validate Zip code using JavaScript Regular expression






“^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$”
Above does not seem to work with a valid word@word.com
So make ‘-word’ and ‘.word’ optional to allow word@word.com
“^[\\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 word@aa.com .
is ‘-word@email.com’ even valid?? Both regex’s will take it as valid.
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.
Thanks for your kind words. I am glad you find my post useful.
That is what I was looking for. It worked liked a charm. Thanks
WARNING: “^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$” Does not work!
- Only supports a hyphen at the begining of the address:
> “-abc@abc.com” works
> “abc-def@abc.com” 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).
The last expression also matches an e-mail like :
“user@dom1.dom2.dom3.dom4″ …
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
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
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.
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
This is very helpful
thanks
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
joel@joel.com pass
joel@joel.edu pass
joel@joel.museum pass
joel@joel.ac.uk pass
joel@guy.joel.ac.uk pass
guy.joel@guy.joel.ac.uk pass
guy.joel@127.0.0.1 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.
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
Can I use your code in my project?
You sure can.