How to validate GUID ( Globally Unique Identifier ) using Regular Expression | C ++ program to validate the GUID ( Globally Unique Identifier ) using Regular Expression ; Function to validate the GUID ( Globally Unique Identifier ) . ; Regex to check valid GUID ( Globally Unique Identifier ) . ; If the GUID ( Globally Unique Identifier ) is empty return false ; Return true if the GUID ( Globally Unique Identifier ) matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidGUID ( string str ) { const regex pattern ( " ^ [ { ] ? [ 0-9a - fA - F ] { 8 } - ( [ 0-9a - fA - F ] { 4 } - ) { 3 } [ 0-9a - fA - F ] { 12 } [ } ] ? $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "123e4567 - e89b - 12d3 - a456-9AC7CBDCEE52" ; cout << isValidGUID ( str1 ) << endl ; string str2 = " { 123e4567 - e89b - 12d3 - a456-9AC7CBDCEE52 } " ; cout << isValidGUID ( str2 ) << endl ; string str3 = "123e4567 - h89b - 12d3 - a456-9AC7CBDCEE52" ; cout << isValidGUID ( str3 ) << endl ; string str4 = "123e4567 - h89b - 12d3 - a456" ; cout << isValidGUID ( str4 ) << endl ; return 0 ; }