How to validate Hexadecimal Color Code using Regular Expression
Last Updated :
26 Dec, 2022
Given 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.
- It should start from ‘#’ symbol.
- It should be followed by the letters from a-f, A-F and/or digits from 0-9.
- 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.
- Get the string.
- 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 hexadecimal color code must start with a ‘#’ symbol.
- ( 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 a 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 a length of 3.
- ) represents the end of the group.
- $ represents the ending of the string.
- Match the given string with the regex, in Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regex, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidHexaCode(string str)
{
const regex pattern( "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "#1AFFa1" ;
cout << str1 + ": " << isValidHexaCode(str1) << endl;
string str2 = "#F00" ;
cout << str2 + ": " << isValidHexaCode(str2) << endl;
string str3 = "123456" ;
cout << str3 + ": " << isValidHexaCode(str3) << endl;
string str4 = "#123abce" ;
cout << str4 + ": " << isValidHexaCode(str4) << endl;
string str5 = "#afafah" ;
cout << str5 + ": " << isValidHexaCode(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidHexaCode(String str)
{
String regex = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "#1AFFa1" ;
System.out.println(
str1 + ": "
+ isValidHexaCode(str1));
String str2 = "#F00" ;
System.out.println(
str2 + ": "
+ isValidHexaCode(str2));
String str3 = "123456" ;
System.out.println(
str3 + ": "
+ isValidHexaCode(str3));
String str4 = "#123abce" ;
System.out.println(
str4 + ": "
+ isValidHexaCode(str4));
String str5 = "#afafah" ;
System.out.println(
str5 + ": "
+ isValidHexaCode(str5));
}
}
|
Python3
import re
def isValidHexaCode( str ):
regex = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "#1AFFa1"
print (str1, ":" , isValidHexaCode(str1))
str2 = "#F00"
print (str2, ":" , isValidHexaCode(str2))
str3 = "123456"
print (str3, ":" , isValidHexaCode(str3))
str4 = "#123abce"
print (str4, ":" , isValidHexaCode(str4))
str5 = "#afafah"
print (str5, ":" , isValidHexaCode(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "#1AFFa1" , "#F00" , "123456" , "#123abce" , "#afafah" };
foreach ( string s in str) {
Console.WriteLine( isValidHexaCode(s) ? "true" : "false" );
}
Console.ReadKey(); }
public static bool isValidHexaCode( string str)
{
string strRegex = @"^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return ( true );
else
return ( false );
}
}
|
Javascript
function isValidHexaCode(str) {
let regex = new RegExp(/^ #([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "#1AFFa1" ;
console.log(isValidHexaCode(str1));
let str2 = "#F00" ;
console.log(isValidHexaCode(str2));
let str3 = "123456" ;
console.log(isValidHexaCode(str3));
let str4 = "#123abce" ;
console.log(isValidHexaCode(str4));
let str5 = "#afafah" ;
console.log(isValidHexaCode(str5));
|
Output:
#1AFFa1: true
#F00: true
123456: false
#123abce: false
#afafah: false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...