Java email regex examples

author image

java email regex

The format of an email address is local-part@domain. Look at this email address mkyong@example.com

  1. local-part = mkyong
  2. @ = @
  3. domain = example.com

The formal definitions of an email address are in RFC 5322 and RFC 3696. However, this article will not follow the above RFC for email validation. The official email "local-part" is too complex (supports too many special characters, symbols, comments, quotes…) to implement via regex. Most companies or websites choose only to allow certain special characters like dot (.), underscore (_), and hyphen (-).

This article will show a few ways to validate an email address via regex:

  1. Email Regex – Simple
  2. Email Regex – Strict
  3. Email Regex – Non-Latin or Unicode characters
  4. Apache Commons Validation v1.7

1. Email Regex – Simple Validation.

This example uses a simple regex ^(.+)@(\S+)$ to validate an email address. It checks to ensure the email contains at least one character, an @ symbol, then a non whitespace character.

Email regex explanation:

                              ^                       #start of the line   (                     #   start of group #1     .+                  #     any characters (matches Unicode), must contains one or more (+)   )                     #   end of group   #1     @                   #     must contains a "@" symbol       (                 #         start of group #2         \S+             #           non white space characters, must contains one or more (+)       )                 #         end of group #2 $