How to validate Hexadecimal Color Code using Regular Expression | C ++ program to validate the hexadecimal color code using Regular Expression ; Function to validate the hexadecimal color code . ; Regex to check valid hexadecimal color code . ; If the hexadecimal color code is empty return false ; Return true if the hexadecimal color code matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE 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 ; }