36 Comments

  1. How to validate email, SSN, phone number in Java using Regular expressions. | zParacha.com
    February 3, 2009 @ 11:20 pm

    […] //Initialize reg ex for email. String expression = “^[w.-]+@([w-]+.)+[A-Z]{2,4}$”; CharSequence inputStr = email; //Make the comparison case-insensitive. Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(inputStr); if(matcher.matches()){ isValid = true; } return isValid; } Update: I came with a more thorough Java regular expression to validate email address. […]

    Reply

  2. Alan Q. Jamison
    February 24, 2009 @ 11:06 am

    “^[\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.

    Reply

  3. How to Get Six Pack Fast
    April 15, 2009 @ 9:49 am

    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.

    Reply

  4. javabug
    April 22, 2009 @ 7:45 am

    That is what I was looking for. It worked liked a charm. Thanks

    Reply

  5. user
    April 27, 2009 @ 12:54 pm

    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).

    Reply

  6. Ame
    July 9, 2009 @ 5:26 am

    The last expression also matches an e-mail like :

    “user@dom1.dom2.dom3.dom4” …

    Reply

  7. Himanshu Gilani
    July 22, 2009 @ 3:48 pm

    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

    Reply

  8. hugmin42
    October 1, 2009 @ 8:39 am

    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

    Reply

  9. MK
    October 27, 2009 @ 2:29 pm

    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.

    Reply

  10. Michael Capper
    December 1, 2009 @ 5:40 am

    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 🙂

    Reply

  11. Muhammad
    December 24, 2009 @ 8:10 am

    This is very helpful

    thanks 😀

    Reply

  12. T0meck
    January 1, 2010 @ 2:46 pm

    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.

    Reply

  13. d0n
    April 15, 2010 @ 3:50 am

    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 🙂

    Reply

  14. Levee Jenkins
    July 17, 2010 @ 12:36 pm

    Can I use your code in my project?

    Reply

  15. Mike
    October 3, 2010 @ 8:06 pm

    Multiple dots pass validation (e.g. user@domain….com)

    Reply

  16. Tulio Domingos
    October 26, 2010 @ 9:36 am

    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

    Reply

  17. Michael
    November 16, 2010 @ 10:23 am

    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:

    ‘/^((?>(?>(?>((?>[ ]+(?>x0Dx0A[ ]+)*)?)(((?>(?2)(?>[x01-x08x0Bx0Cx0E-‘*-[]-x7F]|\[x00-x7F]|(?3)))*(?2))))+(?2))|(?2))?)([!#-‘*+/-9=?^-~-]+|”(?>(?2)(?>[x01-x08x0Bx0Cx0E-!#-[]-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’

    Reply

  18. AnimeElf
    August 22, 2011 @ 10:33 pm

    That was awesome!! Helped me in my assignment!!

    Reply

  19. mark
    September 7, 2011 @ 8:20 am

    my.email@yahoo.com = fail

    Fell at the first hurdle

    Reply

  20. Brad
    October 6, 2011 @ 5:23 pm

    The first example in this post fails with the example

    some+email@gmail.com

    Reply

  21. User
    November 1, 2011 @ 7:10 am

    how to validate email address like gmail@gmail.com i.e it doesnot allow

    Reply

  22. Fabio
    November 16, 2011 @ 10:54 am

    Hi. E-mails with only 2 characters in the beginning are failing.

    ab@gmail.com = fail
    12@gmail.com = fail

    Reply

  23. naman
    December 2, 2011 @ 3:43 am

    exactly what I’m looking for.

    Reply

  24. schön
    December 5, 2011 @ 9:03 am

    schön@grün.de fails – w doesn’t match “umlaute” in java..

    Reply

  25. Seth
    December 8, 2011 @ 2:15 pm

    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}$”;

    Reply

  26. Seth Kaufmann
    December 8, 2011 @ 2:15 pm

    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}$";

    Reply

  27. ryouji_shiki
    December 14, 2011 @ 4:54 pm

    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}))$

    Reply

  28. Moe
    December 23, 2011 @ 5:45 am

    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.

    Reply

  29. Moe
    December 23, 2011 @ 5:54 am

    String EMAIL_PATTERN = “^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[a-z,A-Z]{2,})$”;

    Reply

  30. JSupport
    February 28, 2012 @ 4:32 am

    How to Validate Email Address using Java Pattern

    Validate Email Address using Java Pattern

    Reply

  31. Vlad
    March 6, 2012 @ 2:44 pm

    Doesn’t validate: test@test-test.com

    Reply

  32. Montecchyo
    April 17, 2012 @ 8:28 pm

    Thx for share.

    Reply

  33. bob
    June 28, 2012 @ 6:43 pm

    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.

    Reply

  34. Dennis
    February 20, 2013 @ 10:05 am

    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}”

    Reply

  35. java2novice
    April 9, 2014 @ 4:31 am

    nice. for java examples, visit http://java2novice.com site

    Reply

  36. answersz.com
    December 15, 2014 @ 3:56 am

    Very Useful. For more examples visit http://answersz.com

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

This site uses Akismet to reduce spam. Learn how your comment data is processed.