# How to validate Hexadecimal Color Code using Regular Expression

Given a string str, the task is to check whether the string is valid hexadecimal colour code or not by using Regular Expression.

The valid hexadecimal color code must satisfy the following conditions.

1. It should start from ‘#’ symbol.
2. It should be followed by the letters from a-f, A-F and/or digits from 0-9.
3. The length of the hexadecimal color code should be either 6 or 3, excluding ‘#’ symbol.

For example: #abc, #ABC, #000, #FFF, #000000, #FF0000, #00FF00, #0000FF, #FFFFFF are all valid Hexadecimal color codes.

Examples:

Input: str = “#1AFFa1”;
Output: true
Explanation:
The given string satisfies all the above mentioned conditions.

Input: str = “#F00”;
Output: true
Explanation:
The given string satisfies all the above mentioned conditions.

Input: str = “123456”;
Output: false
Explanation:
The given string doesn’t start with a ‘#’ symbol, therefore it is not a valid hexadecimal color code.

Input: str = “#123abce”;
Output: false
Explanation:
The given string has length 7, the valid hexadecimal color code length should be either 6 or 3. Therefore it is not a valid hexadecimal color code.

Input: str = “#afafah”;
Output: false
Explanation:
The given string contains ‘h’, the valid hexadecimal color code should be followed by the letter from a-f, A-F. Therefore it is not a valid hexadecimal color code.

Approach: This problem can be solved by using Regular Expression.

1. Get the string.
2. Create a regular expression to check valid hexadecimal color code as mentioned below:
```regex = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\$";
```

Where:

• ^ represents the starting of the string.
• ( represents the start of the group.
• [A-Fa-f0-9]{6} represents the letter from a-f, A-F or digit from 0-9 with length of 6.
• | represents the or.
• [A-Fa-f0-9]{3} represents the letter from a-f, A-F or digit from 0-9 with length of 3.
• ) represents end of the group.
• \$ represents the ending of the string.
3. Match the given string with the regex, in Java, this can be done by using Pattern.matcher().
4. Return true if the string matches with given regex, else return false.

Below is the implementation of the above approach:

 `// Java program to validate hexadecimal ` `// colour code using Regular Expression ` ` `  `import` `java.util.regex.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to validate hexadecimal color code . ` `    ``public` `static` `boolean` `isValidHexaCode(String str) ` `    ``{ ` `        ``// Regex to check valid hexadecimal color code. ` `        ``String regex = ``"^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\$"``; ` ` `  `        ``// Compile the ReGex ` `        ``Pattern p = Pattern.compile(regex); ` ` `  `        ``// If the string is empty ` `        ``// return false ` `        ``if` `(str == ``null``) { ` `            ``return` `false``; ` `        ``} ` ` `  `        ``// Pattern class contains matcher() method ` `        ``// to find matching between given string ` `        ``// and regular expression. ` `        ``Matcher m = p.matcher(str); ` ` `  `        ``// Return if the string ` `        ``// matched the ReGex ` `        ``return` `m.matches(); ` `    ``} ` ` `  `    ``// Driver Code. ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` ` `  `        ``// Test Case 1: ` `        ``String str1 = ``"#1AFFa1"``; ` `        ``System.out.println( ` `            ``str1 + ``": "` `            ``+ isValidHexaCode(str1)); ` ` `  `        ``// Test Case 2: ` `        ``String str2 = ``"#F00"``; ` `        ``System.out.println( ` `            ``str2 + ``": "` `            ``+ isValidHexaCode(str2)); ` ` `  `        ``// Test Case 3: ` `        ``String str3 = ``"123456"``; ` `        ``System.out.println( ` `            ``str3 + ``": "` `            ``+ isValidHexaCode(str3)); ` ` `  `        ``// Test Case 4: ` `        ``String str4 = ``"#123abce"``; ` `        ``System.out.println( ` `            ``str4 + ``": "` `            ``+ isValidHexaCode(str4)); ` ` `  `        ``// Test Case 5: ` `        ``String str5 = ``"#afafah"``; ` `        ``System.out.println( ` `            ``str5 + ``": "` `            ``+ isValidHexaCode(str5)); ` `    ``} ` `} `

Output:

```#1AFFa1: true
#F00: true
123456: false
#123abce: false
#afafah: false
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.