Easy learning with example program codes

java regex pattern validate hex code

Regular expressions

Regular expressions represents a sequence of symbols and characters expressing a string or pattern to be searched for within a longer piece of text. The abbreviation for regular expression is regex. In programming regular expressions are mainly used to define constraint on strings like password, email validation.

Regular expression hex code pattern


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.


package com.codesjava;
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 = 
	public static void main(String args[]){ 
		List<String> values = new ArrayList<String>();	
		Pattern pattern = Pattern.compile(PATTERN);
		for (String value : values){
		  Matcher matcher = pattern.matcher(value);
			  System.out.println("Password "+ value +" is valid");
			  System.out.println("Password "+ value +" is invalid");


Password jaisingh1A@ is valid
Password Sjai12$ is valid
Password jaiA@ is invalid
Password jai* is invalid

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki