Home » Core Java » util » regex » Matcher » Validate Email Address with Java Regular Expression example

About Nikos Maravitsas

Nikos Maravitsas
Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Validate Email Address with Java Regular Expression example

Email validation is a very frequent requirement in many applications. Basically the main policy that email format follows is that it:

  • Has to start with characters, digits or ‘_’, ‘-‘, ‘+’ symbols
  • The above group can be followed with a ‘.’ and the same pattern as the first group.
  • Then it must have exactly one ‘@’ character.
  • The domain name must start with characters, digits and the ‘-‘ character.
  • Then it must be followed by a ‘.’.
  • After the ‘.’ you can have characters and digits.
  • Optionally you  can have a second level Top Level Domain that can start with a ‘.’ and the contain only characters.

This is the regular expression used for email validation:

^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

You can take a look at the Pattern class documentation to learn how to construct your own regular expressions according to your policy.

1. Validator class

This is the class that we are going to use for email format validation.

EmailFormatValidator.java:

package com.javacodegeeks.java.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailFormatValidator {

	private Pattern pattern;
	private Matcher matcher;

	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
			+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

	public EmailFormatValidator() {
		pattern = Pattern.compile(EMAIL_PATTERN);
	}

	public boolean validate(final String email) {

		matcher = pattern.matcher(email);
		return matcher.matches();

	}
}

2. Unit Testing our EmailFormatValidator class

For unit testing we are going to use JUnit. Unit testing is very important in these situations because they provide good feedback about the correctness of our regular expressions. You can test your program and reassure that your regular expression meets the rules on your policy about the form of the email addresses.

This is a basic test class:

EmailFormatValidatorTest.java:

package com.javacodegeeks.java.core;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class EmailFormatValidatorTest {

	private String arg;
	private static EmailFormatValidator emailFormatValidator;
	private Boolean expectedValidation;

	public EmailFormatValidatorTest(String str, Boolean expectedValidation) {
		this.arg = str;
		this.expectedValidation = expectedValidation;
	}

	@BeforeClass
	public static void initialize() {
		emailFormatValidator = new EmailFormatValidator();
	}

	@Parameters
	public static Collection<Object[]> data() {
		Object[][] data = new Object[][] {
				{ "[email protected]",false },    // it's not allowed to have a digit in the second level tld
				{ "[email protected]@oracle.com", false },         // you cannot have @ twice in the address
				{ "[email protected]", false },          // you cannot the have special character '!' in the address
				{ "mysite@.com", false },                  // tld cannot start with a dot
				{ "javacodegees.com", false },             // must contain a @ character and a tld
				{ ".[email protected]", false },     // the address cannot start with a dot
				{ "javacodegees..[email protected]", false }, // you cannot have double dots in your address


				{ "[email protected]",true },                         
				{ "[email protected]", true },
				{ "[email protected]", true },
				{ "[email protected]", true } };

		return Arrays.asList(data);
	}

	@Test
	public void test() {
		Boolean res = emailFormatValidator.validate(this.arg);
		String validv = (res) ? "valid" : "invalid";
		System.out.println("Hex Color Code "+arg+ " is " + validv);
		assertEquals("Result", this.expectedValidation, res);

	}

}

Output:

Hex Color Code [email protected] is invalid
Hex Color Code [email protected]@oracle.com is invalid
Hex Color Code [email protected] is invalid
Hex Color Code mysite@.com is invalid
Hex Color Code javacodegees.com is invalid
Hex Color Code .[email protected] is invalid
Hex Color Code javacodegees..[email protected] is invalid
Hex Color Code [email protected] is valid
Hex Color Code [email protected] is valid
Hex Color Code [email protected] is valid
Hex Color Code [email protected] is valid

 
This was an example on how to perform email address format validation with Java Regular Expression.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns