java regex pattern validate email

Regular expression email pattern


This regular expression refers to a pattern which must start with “_A-Za-z0-9-\+” , optional follow by “.[_A-Za-z0-9-]”, and end with a “@” symbol. The email’s domain name must start with “A-Za-z0-9-“, follow by first level Tld (.com, .net) “.[A-Za-z0-9]” and optional follow by a second level Tld (, “\.[A-Za-z]{2,}”, where second level Tld must start with a dot “.” and length must equal or more than 2 characters.


package com.w3spoint;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
	private static final String PATTERN = 
			+ "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
	public static void main(String args[]){ 
		List<String> values = new ArrayList<String>();	
		values.add("[email protected]"); 
		values.add("[email protected]"); 
		values.add("[email protected]");
		values.add("[email protected]");
		Pattern pattern = Pattern.compile(PATTERN);
		for (String value : values){
		  Matcher matcher = pattern.matcher(value);
			  System.out.println("Email "+ value +" is valid");
			  System.out.println("Email "+ value +" is invalid");


Email [email protected]com is valid
Email [email protected]com is valid
Email [email protected]com is invalid
Email [email protected]com.a is invalid
Please Share