GUID Regex Go Validator

Search...

⌘K

GUID Regex Go Validator

Search...

⌘K


GUID Regex Go Validator

GUID Regex Go Validator

Use the GUID Regex Go Validator to ensure your unique identifiers follow the correct structure. Test and debug your regex in the Go Regex Tester or validate associated data like emails, URLs, and passwords for a complete data quality workflow.

deadbeef-7331-4123-8123-ba5eba11babe
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

Introduction: What Is GUID Regex?

In Go (Golang), a GUID (Globally Unique Identifier), also known as a UUID (Universally Unique Identifier), is used to identify resources uniquely across distributed systems. It ensures that no two values are ever the same, making it vital for tracking, versioning, and referencing.


A GUID looks like this:

3f2504e0-4f89-11d3-9a0c-0305e82c3301


It contains 32 hexadecimal characters arranged into 5 groups separated by hyphens, following the pattern:


8-4-4-4-12


The most reliable way to validate GUID formats in Go is using regular expressions (regex). Regex allows you to match and verify this structure before storing or processing the data.


GUID Regex Pattern (with Breakdown)


Here’s the regex pattern we use to validate a GUID:


^[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}$


Explanation:


  • [0-9a-fA-F]{8} – First group of 8 hexadecimal digits

  • - – Hyphen separator

  • [0-9a-fA-F]{4} – Second group of 4 digits

  • [1-5][0-9a-fA-F]{3} – Version digit (1–5) + 3 more hex digits

  • [89abAB][0-9a-fA-F]{3} – Variant digit (starting with 8, 9, a, or b) + 3 hex digits

  • [0-9a-fA-F]{12} – Final 12 hex digits


Are Braces Allowed in GUIDs?


While the standard GUID format uses hyphens to separate its five groups, you may also encounter GUIDs enclosed in curly braces—especially in Microsoft and certain Windows environments. For example:

  • With braces: {3f2504e0-4f89-11d3-9a0c-0305e82c3301}

  • Without braces: 3f2504e0-4f89-11d3-9a0c-0305e82c3301

The regex pattern provided above is robust enough to handle both scenarios. Notice the optional { and } at the beginning and end of the pattern. This means your GUID can be validated whether it’s wrapped in braces or not, ensuring compatibility with tools, APIs, and libraries that might expect either style. This flexibility is key when processing data that originates from various systems, like Windows, .NET-based frameworks, or cross-platform applications.


Auxiliary Space Complexity

Validating a GUID with a regular expression in Go is highly efficient in terms of memory use. The auxiliary space complexity is O(1)—that is, checking whether a string matches the GUID pattern only requires a fixed, constant amount of extra memory, regardless of the length of the string. All operations happen in-place, and no additional data structures are created during validation. This makes regex-based GUID validation a lightweight and performant choice within your applications.


Performance: Time Complexity of GUID Validation

When you validate a GUID using a regular expression, the process generally scans the string once from start to finish to check for pattern compliance. This means the time complexity is linear with respect to the length of your input string, or O(N), where N is the number of characters in the candidate GUID. No extra memory is needed beyond a few bookkeeping variables, so auxiliary space remains O(1).

In short: regex-based GUID checks are efficient—you’ll rarely notice any delay, even at scale.


How to Validate GUIDs in Go Using Regex


You can validate GUIDs using the regexp package in Go. Here’s the complete Go code:


package main

import (
    "fmt"
    "regexp"
)

func isValidGUID(guid string) bool {
    pattern := `^[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}$`
    regex := regexp.MustCompile(pattern)
    return regex.MatchString(guid)
}

func main() {
    samples := []string{
        "3f2504e0-4f89-11d3-9a0c-0305e82c3301", // valid
        "3F2504E0-4F89-11D3-9A0C-0305E82C3301", // valid uppercase
        "123e4567e89b12d3a456426614174000",     // invalid (no hyphens)
        "3f25-04e0-4f89-11d3-9a0c-0305e82c3301", // invalid (group mismatch)
    }

    for _, guid := range samples {
        fmt.Printf("GUID: %s | Valid: %t\n", guid, isValidGUID(guid))
    }
}


Real-World Use Cases


  • Database Primary Keys: Useful for identifying records in distributed databases.

  • API Resource Identifiers: RESTful services use GUIDs for resources like /user/3f2504e0-4f89-11d3-9a0c-0305e82c3301.

  • Session Tokens: Web apps often use UUIDs to track secure sessions.

  • Device or File IDs: Many cloud storage services use GUIDs to uniquely reference files.


Pro Tips


  • Always use lowercase or uppercase consistently when displaying GUIDs.

  • Regex validates the format only — not whether the GUID is truly unique.

  • Never expose GUIDs that relate to sensitive resources in unsecured URLs.

  • Use regex for validation but libraries like github.com/google/uuid for generation.

  • For batch testing, use the Go Regex Tester to try different formats quickly.



How to Validate GUIDs in JavaScript


If you need to ensure your strings conform to the standard GUID (Globally Unique Identifier) format in JavaScript, you can use a regular expression for a quick check. GUIDs are 128-bit values often represented as 32 hexadecimal digits, displayed in five groups separated by hyphens, for example: 3f2504e0-4f89-11d3-9a0c-0305e82c3301.

Here’s a straightforward function to validate GUIDs in JavaScript:

function isValidGUID(input) {
  // Regex matches an optional opening brace, 8 hex digits, 3 groups of 4 hex digits, then 12 hex digits, ending with an optional closing brace.
  Const guidPattern = /^[{]?[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}[}]?$/;
  return guidPattern.test(input);
}

Usage Examples

  • isValidGUID('3f2504e0-4f89-11d3-9a0c-0305e82c3301') returns true.

  • isValidGUID('{3f2504e0-4f89-11d3-9a0c-0305e82c3301}') also returns true (with curly braces).

  • isValidGUID('invalid-guid-value') returns false.

With this validation, you can quickly filter or reject malformed identifiers right inside your JavaScript workflow—perfect for when data quality needs to be rock-solid.


Validating GUIDs in Python


Just like in Go, you’ll often need to ensure your GUIDs are in the correct format within Python-based systems. Thankfully, Python’s re module makes regex validation simple and reliable.

Below is a straightforward approach to GUID validation in Python:

import re

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

A few highlights to notice:

  • Pattern overview: This regex enforces the proper structure: 8-4-4-4-12 hex characters, hyphen-separated.

  • Case-insensitive: Both uppercase and lowercase hexadecimal digits are accepted.

  • No curly braces: This example strictly validates standard, hyphenated GUIDs—expand the pattern if you need to accept optional braces.

Let’s see it in action. Here are some sample inputs and expected outcomes:

test_guids = [
    "3f2504e0-4f89-11d3-9a0c-0305e82c3301",     # valid
    "3F2504E0-4F89-11D3-9A0C-0305E82C3301",     # valid (uppercase)
    "123e4567e89b12d3a456426614174000",         # invalid (no hyphens)
    "3f25-04e0-4f89-11d3-9a0c-0305e82c3301",    # invalid (incorrect groups)
]

for guid in test_guids:
    print(f"GUID: {guid} Valid: {is_valid_guid(guid)}")

This flexible approach is well-suited to API endpoints, form validation, and data cleaning tasks in any Python application.


Implementing GUID Validation in Java


If you're working in Java and need to validate GUIDs (also known as UUIDs), you can use regular expressions much like you would in Go. Java’s built-in Pattern and Matcher classes make this straightforward.

Here's a practical example of how to create a GUID validation utility in Java:

import java.util.regex.Pattern;

public class GuidValidator {
    // GUID pattern, allowing curly braces (optional) around the GUID
    private static final Pattern GUID_PATTERN = Pattern.compile(
        "^[{]?[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}[}]?$"
    );

    /**
     * Checks if the given string is a valid GUID.
     * 
     * @param guid The string to validate.
     * @return true if valid, false otherwise.
     */
    public static boolean isValidGuid(String guid) {
        return guid != null && GUID_PATTERN.matcher(guid).matches();
    }

    // Quick test
    public static void main(String[] args) {
        String[] testGuids = {
            "123e4567-e89b-12d3-a456-9AC7CBDCEE52", // valid
            "{123e4567-e89b-12d3-a456-9AC7CBDCEE52}", // valid with curly braces
            "123e4567-h89b-12d3-a456-9AC7CBDCEE52", // invalid (contains 'h')
            "123e4567-e89b-12d3-a456" // invalid (too short)
        };
        for (String guid : testGuids) {
            System.out.printf("GUID: %s Valid: %b%n", guid, isValidGuid(guid));
        }
    }
}

Key points:

  • The regular expression checks for the standard GUID format (optionally wrapped in curly braces).

  • It ensures correct grouping and valid hexadecimal characters.

  • Returns true only if the format matches exactly; case is insensitive for hex digits.

You can easily adapt this to fit your own classes or integrate it into your validation pipelines, whether you're processing user submissions, working with API inputs, or handling any kind of uniquely identified data in Java.


C# Example: Validating GUIDs with Regex


Need to ensure your GUIDs are properly formatted in C#? Regex makes it straightforward. Here’s how you can check whether a string matches the GUID pattern using the System.Text.RegularExpressions namespace.

Step-by-Step Implementation

  1. Define the Regex Pattern
    A typical GUID format is: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, where each x is a hexadecimal digit. Sometimes, GUIDs may include curly braces {}.

  2. Write a Validation Method
    You can encapsulate the logic in a reusable function:

    using System.Text.RegularExpressions;
    
    public static bool IsValidGuid(string input)
    {
        var pattern = @"^[{]?[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}[}]?$";
        return Regex.IsMatch(input, pattern
    
    
  3. Try It Out with Example GUIDs

    var testInputs = new[]
    {
        "123e4567-e89b-12d3-a456-9AC7CBDCEE52",
        "{123e4567-e89b-12d3-a456-9AC7CBDCEE52}",
        "invalid-guid-string",
        "123e4567-h89b-12d3-a456"
    };
    
    foreach (var guid in testInputs)
    {
        Console.WriteLine(IsValidGuid(guid)); // Outputs true or false
    
    

Pro tip: This approach ensures only well-formed GUIDs pass validation—helping you maintain reliable identifiers and catch accidental typos early.

Now, let's see how you can use similar principles for API testing and documentation:


C++ Example: Validating GUIDs with Regular Expressions


If you’re working in C++ and need to check whether a string conforms to the typical GUID format, regular expressions are your friend—just like in Go. The <regex> standard library makes this process straightforward.

Here’s a concise implementation for GUID validation in C++:

#include 
#include 
#include 

bool isValidGUID(const std::string& guid) {
    // This pattern allows an optional set of curly braces around the GUID.
    Const std::regex pattern(R"(^\{?[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}\}?$)");
    return !guid.empty() && std::regex_match(guid, pattern);
}

int main() {
    std::string samples[] = {
        "123e4567-e89b-12d3-a456-9AC7CBDCEE52",     // valid
        "{123e4567-e89b-12d3-a456-9AC7CBDCEE52}",   // valid with braces
        "123e4567-h89b-12d3-a456-9AC7CBDCEE52",     // invalid - contains non-hex 'h'
        "123e4567-h89b-12d3-a456"                   // invalid - incomplete
    };
    for (const auto& guid : samples) {
        std::cout << "GUID: " << guid
                  << " Valid: " << std::boolalpha << isValidGUID(guid) << std::endl;
    }
    return 0;
}

How it works:

  • The regular expression enforces the GUID structure (8-4-4-4-12 hex digits, optionally wrapped in braces).

  • The function returns true only if the string isn’t empty and exactly matches the GUID pattern.

  • You can easily expand this function to handle variant and version checks for stricter validation, but for most practical cases, this pattern is sufficient.

This method ensures your C++ code robustly validates GUIDs before accepting them as legitimate identifiers in your application.




Combine with These Tools


Use the GUID Regex Go Validator alongside these tools to ensure your data pipeline is complete:


Frequently asked questions

What’s the difference between GUID and UUID?×
GUID and UUID are functionally the same — GUID is used in Microsoft ecosystems, while UUID is the formal RFC term.
Can this regex detect fake or duplicate GUIDs?+
Does this regex validate all versions of UUIDs?+
Are uppercase letters in GUIDs valid?+
Can I remove hyphens from the regex?+