# How to validate GUID (Globally Unique Identifier) using Regular Expression

Given a string str, the task is to check whether the given string is valid GUID (Globally Unique Identifier) or not by using Regular Expression.

The valid GUID (Globally Unique Identifier) must specify the following conditions:

1. It shold be 128 bit number.
2. It should be 36 characters (32 hexadecimal characters and 4 hyphens) long.
3. It should be displayed in five groups separated by hyphens (-).
4. Microsoft GUIDs are sometimes represented with surrounding braces.

Examples:

Input: str = “123e4567-e89b-12d3-a456-9AC7CBDCEE52”
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore it is a valid GUID (Globally Unique Identifier).

Input: str = “123e4567-h89b-12d3-a456-9AC7CBDCEE52”
Output: false
Explanation:
The given string contains ‘h’, the valid hexadecimal characters should be followed by character from a-f, A-F, and 0-9. Therefore it is not a valid GUID (Globally Unique Identifier).

Input: str = “123e4567-h89b-12d3-a456”
Output: false
Explanation:
The given string has 20 characters. Therefore it is not a valid GUID (Globally Unique Identifier).

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use Regular Expression to sove this problem. The following steps can be followed to compute the answer:

1. Get the String.
2. Create a regular expression to check valid GUID (Globally Unique Identifier) as mentioned below:

regex = “^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?\$”

Where:

• ^ represents the starting of the string.
• [{]? represents the ‘{‘ character that is optional.
• [0-9a-fA-F]{8} represents the 8 characters from a-f, A-F, and 0-9.
• represents the hyphens.
• ([0-9a-fA-F]{4}-){3} represents the 4 characters from a-f, A-F, and 0-9 that is repeated 3 times separated by a hyphen (-).
• [0-9a-fA-F]{12} represents the 12 characters from a-f, A-F, and 0-9.
• [}]? represents the ‘}’ character that is optional.
• \$ represents the ending of the string.
3. Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
4. Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:

## Java

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

Output:

```true
true
false
false
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.