How to validate GUID ( Globally Unique Identifier ) using Regular Expression | Java program to validate GUID ( Globally Unique Identifier ) using regular expression ; Function to validate GUID ( Globally Unique Identifier ) using regular expression ; Regex to check valid GUID ( Globally Unique Identifier ) ; Compile the ReGex ; If the string is empty return false ; Find match between given string and regular expression uSing Pattern . matcher ( ) ; Return if the string matched the ReGex ; Driver code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
import java . util . regex . * ; class GFG { public static boolean isValidGUID ( String str ) { String regex = " ^ [ { ] ? [ 0-9a - fA - F ] { 8 } " + " - ( [ 0-9a - fA - F ] { 4 } - ) " + " { 3 } [ 0-9a - fA - F ] { 12 } [ } ] ? $ " ; 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 str2 = "123e4567 - e89b - 12d3" + " - a456-9AC7CBDCEE52" ; System . out . println ( isValidGUID ( str2 ) ) ; String str3 = " { 123e4567 - e89b - 12d3 - " + " a456-9AC7CBDCEE52 } " ; System . out . println ( isValidGUID ( str3 ) ) ; String str1 = "123e4567 - h89b - 12d3 - a456" + " - 9AC7CBDCEE52" ; System . out . println ( isValidGUID ( str1 ) ) ; String str4 = "123e4567 - h89b - 12d3 - a456" ; System . out . println ( isValidGUID ( str4 ) ) ; } }