How to validate GUID ( Globally Unique Identifier ) using Regular Expression | Python3 program to validate GUID ( Globally Unique Identifier ) using regular expression ; Function to validate GUID ( Globally Unique Identifier ) ; Regex to check valid GUID ( Globally Unique Identifier ) ; Compile the ReGex ; If the string is empty return false ; Return if the string matched the ReGex ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
import re NEW_LINE def isValidGUID ( str ) : NEW_LINE INDENT regex = " ^ [ { ] ? [ 0-9a - fA - F ] {8 } " + " - ( [ 0-9a - fA - F ] {4 } - ) " + " { 3 } [ 0-9a - fA - F ] {12 } [ } ] ? $ " NEW_LINE p = re . compile ( regex ) NEW_LINE if ( str == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( re . search ( p , str ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT str1 = "123e4567 - e89b - 12d3" + " - a456-9AC7CBDCEE52" NEW_LINE print ( isValidGUID ( str1 ) ) NEW_LINE str2 = " { 123e4567 - e89b - 12d3 - " + " a456-9AC7CBDCEE52 } " NEW_LINE print ( isValidGUID ( str2 ) ) NEW_LINE str3 = "123e4567 - h89b - 12d3 - a456" + " - 9AC7CBDCEE52" NEW_LINE print ( isValidGUID ( str3 ) ) NEW_LINE str4 = "123e4567 - h89b - 12d3 - a456" NEW_LINE print ( isValidGUID ( str4 ) ) NEW_LINE