Validate Complex Password with Regular Expression in Java


Create new java file named Main.java. This file use Regular Expression check validate complex password as below:

package regular_expression;

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

public class Main {

	private static boolean isValid(String password) {
		String complexPasswordPattern = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
		Pattern pattern = Pattern.compile(complexPasswordPattern, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(password);
		return matcher.matches();
	}

	public static void main(String[] args) {

		String password1 = "abc123";
		boolean result1 = isValid(password1);
		System.out.println("Result 1: " + result1);

		String password2 = "abc12D@";
		boolean result2 = isValid(password2);
		System.out.println("Result 2: " + result2);

	}

}




Result 1: false
Result 2: true