java regex pattern validate hex code

Regular expression hex code pattern

#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

This regular expression refers to a pattern which must start with a “#” symbol, follow by a letter from “a” to “f”, “A” to “Z” or a digit from “0” to 9″ with exactly 6 or 3 length.

Example

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 = 
			"((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,15})";
	public static void main(String args[]){ 
		List<String> values = new ArrayList<String>();	
		values.add("jaisingh1A@"); 
		values.add("Sjai12$"); 
		values.add("jaiA@");
		values.add("jai*");
 
		Pattern pattern = Pattern.compile(PATTERN);
		for (String value : values){
		  Matcher matcher = pattern.matcher(value);
		  if(matcher.matches()){
			  System.out.println("Password "+ value +" is valid");
		  }else{
			  System.out.println("Password "+ value +" is invalid");
		  }		  
		}
	}
}

Output

Password jaisingh1A@ is valid
Password Sjai12$ is valid
Password jaiA@ is invalid
Password jai* is invalid
Please follow and like us:
Content Protection by DMCA.com