Regex for GUID in Java, Angular, React, C, Python, VueJS

A Globally Unique Identifier (GUID) is a unique reference number used in software development to identify resources or components.

In this article, we will be discussing the concept of GUID and its structure. Further, we will explore the use of regular expressions (regex) to validate GUIDs in various programming languages and frameworks such as JavaScript, React, Angular, Vue, Python, Java, C++, and C.



What is GUID?

A Globally Unique Identifier (GUID) is a 128-bit integer used to uniquely identify resources, objects, or components in software development.

It is represented as a 32-character hexadecimal string, separated into five groups by hyphens. GUID is used in various applications, such as databases, distributed systems, and component-based software architectures. It ensures that each identifier is unique across systems and networks.


Structure of GUID

The standard representation of a GUID consists of 32 hexadecimal digits, separated into five groups by hyphens, enclosed in curly braces. The format is as follows:


What is contained is:

  • x: any hexadecimal digit (0-9, A-F)
  • M: a 4-bit version number
  • N: a 3-bit variant number


A regular expression or REGEX is a sequence of characters representing a search pattern, mainly used for pattern matching with strings. It includes various elements, such as literals, metacharacters, and quantifiers, which help to define the pattern.


Using Regex for GUID Validation

To validate a GUID using regex, we can use the following pattern:


This pattern covers the standard GUID format, including optional curly braces and hyphens.


Examples of Regex for GUID

Following are various programming languages an framework examples in which we can us the GUID regex pattern to verify its validity:



In JavaScript, we can use the RegExp object to validate a GUID. Here’s an example:

function isValidGUID(guid) {
  const regex = /^\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[1-5][0-9a-fA-F]{3}-?[89ABab][0-9a-fA-F]{3}-?[0-9a-fA-F]{12}\}?$/i;
  return regex.test(guid);

const guid = '{6B29FC40-CA47-1067-B31D-00DD010662DA}';
console.log(isValidGUID(guid)); // true



Inside the React component, validation of GUID can be done as shown below:

import React from 'react';

function isValidGUID(guid) {
  const regex = /^\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[1-5][0-9a-fA-F]{3}-?[89ABab][0-9a-fA-F]{3}-?[0-9a-fA-F]{12}\}?$/i;
  return regex.test(guid);

function App() {
  const guid = '{6B29FC40-CA47-1067-B31D-00DD010662DA}';
  return (
      <p>GUID Validation: {isValidGUID(guid) ? 'Valid' : 'Invalid'}</p>

export default App;



In Angular, you can use the same regex pattern to validate a GUID. Here’s an example using a TypeScript component:

import { Component } from '@angular/core';

  selector: 'app-root',
  template: `
      <p>GUID Validation: {{ isValidGUID(guid) ? 'Valid' : 'Invalid' }}</p>
export class AppComponent {
  guid = '{6B29FC40-CA47-1067-B31D-00DD010662DA}';

  isValidGUID(guid: string): boolean {
    const regex = /^\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[1-5][0-9a-fA-F]{3}-?[89ABab][0-9a-fA-F]{3}-?[0-9a-fA-F]{12}\}?$/i;
    return regex.test(guid);



In a Vue.js component, you can validate a GUID similarly to other frameworks:

    <p>GUID Validation: {{ validateGUID(guid) ? 'Valid' : 'Invalid' }}</p>

export default {
  data() {
    return {
      guid: '{6B29FC40-CA47-1067-B31D-00DD010662DA}',
  methods: {
    validateGUID(guid) {
      const regexPattern = /^\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[1-5][0-9a-fA-F]{3}-?[89ABab][0-9a-fA-F]{3}-?[0-9a-fA-F]{12}\}?$/i;
      return regexPattern.test(guid);



In Python, we can use the <span class="hljs-string">re</span> module to work with regular expressions:

import re

def is_valid_guid(guid):
    regex = r'^\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[1-5][0-9a-fA-F]{3}-?[89ABab][0-9a-fA-F]{3}-?[0-9a-fA-F]{12}\}?$'
    return bool(re.match(regex, guid))

guid = '{6B29FC40-CA47-1067-B31D-00DD010662DA}'
print(is_valid_guid(guid)) # True



In Java, use the java.util.regex package to work with regular expressions:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class GUIDValidation {

    public static boolean isValidGUID(String guid) {
        String regex = "^\\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[1-5][0-9a-fA-F]{3}-?[89ABab][0-9a-fA-F]{3}-?[0-9a-fA-F]{12}\\}?$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(guid);
        return matcher.matches();

    public static void main(String[] args) {
        String guid = "{6B29FC40-CA47-1067-B31D-00DD010662DA}";
        System.out.println(isValidGUID(guid)); // true



In C++, we use the <regex> library to work with regular expressions:

#include <iostream>
#include <regex>
#include <string>

bool isValidGUID(const std::string& guid) {
  std::regex regex(R"(^\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[1-5][0-9a-fA-F]{3}-?[89ABab][0-9a-fA-F]{3}-?[0-9a-fA-F]{12}\}?$)");
  return std::regex_match(guid, regex);

int main() {
  std::string guid = "{6B29FC40-CA47-1067-B31D-00DD010662DA}";
  std::cout << (isValidGUID(guid) ? "Valid" : "Invalid") << std::endl; // Valid



While C does not have a built-in regular expression library, you can use third-party libraries like PCRE to work with regex. Here’s an example of validating a GUID using PCRE:

#include <stdio.h>
#include <stdbool.h>
#include <pcre.h>

bool isValidGUID(const char* guid) {
    const char* regex = "^\\{?[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[1-5][0-9a-fA-F]{3}-?[89ABab][0-9a-fA-F]{3}-?[0-9a-fA-F]{12}\\}?$";
    pcre* compiled_regex;
    const char* error;
    int error_offset;

    compiled_regex = pcre_compile(regex, 0, &error, &error_offset, NULL);
    if (compiled_regex == NULL) {
        printf("PCRE compilation failed at offset %d: %s\n", error_offset, error);
        return false;

    int result = pcre_exec(compiled_regex, NULL, guid, (int)strlen(guid), 0, 0, NULL, 0);

    return result >= 0;

int main() {
    const char* guid = "{6B29FC40-CA47-1067-B31D-00DD010662DA}";
    printf("GUID Validation: %s\n", isValidGUID(guid) ? "Valid" : "Invalid"); // Valid



In this article, we have discussed the concept of GUID, its structure, and the use of regular expressions to validate GUIDs in various programming languages and frameworks.

You can easily use the above-provided examples and efficiently validate GUIDs in JavaScript, React, Angular, Vue, Python, Java, C++, and C.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *