How to validate multiple email addresses with regular expression in Java

In this example I have demonstrated the use of the regular expressions in Java programming language to do a validation of the email address. The email address might be just one or multiples selected by a semi colon or a comma. I also have presented numerous test cases of the email addresses and then have finally provided the output of running the program, as usual.

package com.icodejava.tools;

/**
 * @author Kushal Paudyal
 * Last Modified on 2011/05/11
 * 
 * This class demonstrates the use of regular expressions to validate multiple email
 * addresses. Several test examples of the email addresses are also provided.
 * 
 * www.icodejava.com
 */
import java.util.regex.Pattern;

public class RegexMultipleEmailAddressValidation {
	/**
	 * Define a regular expression to validate multiple email addresses.
	 * I think the following expression is more complex that it has to be.
	 * We can think of refactoring it at some later point. But it works 
	 * just fine right now.
	 */
	public static String regex = 
		"(([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4}))(((;|,|; | ;| ; | , | ,){1}"
		+"([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4}))*)";

	public static void main(String[] args) {
		/**
		 * Validate various email addresses
		 */
		isValid("billgates@badmicrosoft.com");
		isValid("billgates@nation.wide.com");
		isValid("123@badmicrosoft.com");
		isValid("'billgates@badmicrosoft.com'");
		isValid("billgates@badmicrosoft.123");
		isValid("abc.def@badmicrosoft.com");
		isValid("abc.\\def@badmicrosoft.com");
		isValid("abc_def@badmicrosoft.com");
		isValid("abc_def.@badmicrosoft.com");
		isValid("abc_def-@badmicrosoft.com");
		isValid("^billgates@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com;noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com,noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com;;noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com;,noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com; noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com ; noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com , noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com,,noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.com  noreply@badmicrosoft.com");
		isValid("billgates@badmicrosoft.verybad@microsoft.com");

	}

	/**
	 * @param email - The Email Address to be validated
	 * @return true if the email address is valid, else return false.
	 * 
	 * Uses the regular expression defined above to do the validation.
	 */
	public static boolean isValid(String email) {
		System.out.println(email + " >>>>>>>> is Valid? " + Pattern.matches(regex, email));
		return Pattern.matches(regex, email);
	}

}


Here is the output of this program:

billgates@badmicrosoft.com >>>>>>>> is Valid? true
billgates@nation.wide.com >>>>>>>> is Valid? true
123@badmicrosoft.com >>>>>>>> is Valid? true
'billgates@badmicrosoft.com' >>>>>>>> is Valid? false
billgates@badmicrosoft.123 >>>>>>>> is Valid? false
abc.def@badmicrosoft.com >>>>>>>> is Valid? true
abc.\def@badmicrosoft.com >>>>>>>> is Valid? false
abc_def@badmicrosoft.com >>>>>>>> is Valid? true
abc_def.@badmicrosoft.com >>>>>>>> is Valid? true
abc_def-@badmicrosoft.com >>>>>>>> is Valid? true
^billgates@badmicrosoft.com >>>>>>>> is Valid? false
billgates@badmicrosoft.com;noreply@badmicrosoft.com >>>>>>>> is Valid? true
billgates@badmicrosoft.com,noreply@badmicrosoft.com >>>>>>>> is Valid? true
billgates@badmicrosoft.com;;noreply@badmicrosoft.com >>>>>>>> is Valid? false
billgates@badmicrosoft.com;,noreply@badmicrosoft.com >>>>>>>> is Valid? false
billgates@badmicrosoft.com; noreply@badmicrosoft.com >>>>>>>> is Valid? true
billgates@badmicrosoft.com ; noreply@badmicrosoft.com >>>>>>>> is Valid? true
billgates@badmicrosoft.com , noreply@badmicrosoft.com >>>>>>>> is Valid? true
billgates@badmicrosoft.com,,noreply@badmicrosoft.com >>>>>>>> is Valid? false
billgates@badmicrosoft.com  noreply@badmicrosoft.com >>>>>>>> is Valid? false
billgates@badmicrosoft.verybad@microsoft.com >>>>>>>> is Valid? false

How to validate date in Java using Regular Expressions

The following java program has the capability of doing the date format validation in software using regular expressions and is able to validate both mmddyyyy and ddmmyyyy date formats (of course the date, month and year can be separated using punctuations or delimiters).

package com.kushal.tools;
/**
 * @author Kushal Paudyal
 * Last Modified on 2010/12/21
 * 
 * This class demonstrates the use of regular expressions to validate Dates
 * It basically validates two formats of dates:MMDDYYYY & DDMMYYYY
 * Both of these validations accept -, / and . as delimiters.
 * 
 * www.sanjaal.com/java
 */

import java.util.regex.Pattern;

public class DateValidatorRegex {
	
	static String regexMMDDYYYY="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d$";
	static String regexDDMMYYYY="^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$";
	
	public static void main (String [] args ) {
		/**Testing for MMDDYYYY**/
		System.out.println("TEST FOR MMDDYYYY");
		System.out.println(isAValidMMDDYYYYDate("07-20-1982"));//true
		System.out.println(isAValidMMDDYYYYDate("07/20/1982"));//true
		System.out.println(isAValidMMDDYYYYDate("07.20.1982"));//true
		System.out.println(isAValidMMDDYYYYDate("07201982"));//false
		System.out.println(isAValidMMDDYYYYDate("20.07.1982"));//false
		
		/**Testing for MMDDYYYY**/
		System.out.println("\nTEST FOR DDMMYYYY");
		System.out.println(isAValidDDMMYYYYDate("20-07-1982"));//true
		System.out.println(isAValidDDMMYYYYDate("20/07/1982"));//true
		System.out.println(isAValidDDMMYYYYDate("20.07.1982"));//true
		System.out.println(isAValidDDMMYYYYDate("20071982"));//false
		System.out.println(isAValidDDMMYYYYDate("07.20.1982"));//false
		
	}
	/**
	 * @param date
	 * MMDDYYYY DATE VALIDATION
	 * It can validate the following three kinds of dates:
	 * 07-20-1982
	 * 07/20/1982
	 * 07.20.2982
	 */
	public static boolean isAValidMMDDYYYYDate(String date) {
		return Pattern.matches(regexMMDDYYYY, date);
	}
	
	/**
	 * @param date
	 * DDMMYYYY DATE VALIDATION
	 * It can validate the following three formats of dates:
	 * 20-07-1982
	 * 20/07/1982
	 * 20.07.1982
	 */
	public static boolean isAValidDDMMYYYYDate(String date)	{
		return Pattern.matches(regexDDMMYYYY, date);
	}
}