How to validate GUID (Globally Unique Identifier) using Regular Expression
Last Updated :
19 Dec, 2022
Given string str, the task is to check whether the given string is a valid GUID (Globally Unique Identifier) or not by using Regular Expression.
The valid GUID (Globally Unique Identifier) must specify the following conditions:
- It should be a 128-bit number.
- It should be 36 characters (32 hexadecimal characters and 4 hyphens) long.
- It should be displayed in five groups separated by hyphens (-).
- 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).
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer:
- Get the String.
- 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.
- Match the given string with the Regular Expression. In Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
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;
}
|
Java
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));
}
}
|
Python3
import re
def isValidGUID( str ):
regex = "^[{]?[0-9a-fA-F]{8}" + "-([0-9a-fA-F]{4}-)" + "{3}[0-9a-fA-F]{12}[}]?$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "123e4567-e89b-12d3" + "-a456-9AC7CBDCEE52"
print (isValidGUID(str1))
str2 = "{123e4567-e89b-12d3-" + "a456-9AC7CBDCEE52}"
print (isValidGUID(str2))
str3 = "123e4567-h89b-12d3-a456" + "-9AC7CBDCEE52"
print (isValidGUID(str3))
str4 = "123e4567-h89b-12d3-a456"
print (isValidGUID(str4))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "123e4567-e89b-12d3-a456-9AC7CBDCEE52" , "{123e4567-e89b-12d3-a456-9AC7CBDCEE52}" , "123e456-h89b-12d3-a456-9AC7CBDCEE52" , "123e4567-h89b-12d3-a456" };
foreach ( string s in str) {
Console.WriteLine( isValidGUID(s) ? "true" : "false" );
}
Console.ReadKey(); }
public static bool isValidGUID( string str)
{
string strRegex = @"^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return ( true );
else
return ( false );
}
}
|
Javascript
function isValidGUID(str) {
let regex = new RegExp(/^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "123e4567-e89b-12d3"
+ "-a456-9AC7CBDCEE52" ;
console.log(isValidGUID(str1));
let str2 = "{123e4567-e89b-12d3-"
+ "a456-9AC7CBDCEE52}" ;
console.log(isValidGUID(str2));
let str3 = "123e4567-h89b-12d3-a456"
+ "-9AC7CBDCEE52" ;
console.log(isValidGUID(str3));
let str4 = "123e4567-h89b-12d3-a456" ;
console.log(isValidGUID(str4));
|
Output:
true
true
false
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...