UUID Regex Java Validator

Search...

⌘K

UUID Regex Java Validator

Search...

⌘K


UUID Regex Java Validator

UUID Regex Java Validator

The UUID Regex Java Validator is designed to help developers ensure their UUIDs (Universally Unique Identifiers) are formatted correctly in Java applications. From backend services to distributed systems and databases, UUIDs are essential for uniquely identifying entities without collision.

Looking for more Java-based validation tools? Try:


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
Match 1: "deadbeef-7331-4123-8123-ba5eba11babe" at index 0
Test your APIs today!

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

Regular Expression - Documentation

What is a UUID?

A UUID (or GUID) is a 128-bit number used to uniquely identify data. It is represented as a 36-character string with five sections separated by hyphens:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

  • M indicates the UUID version (1–5)

  • N indicates the variant (usually 8, 9, A, or B in hex)


UUIDs are heavily used in APIs, databases, distributed systems, and resource tagging to avoid collisions across systems.

Java Regex Pattern for UUID


"^[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 Explanation:

  • [0-9a-fA-F]{8}: First group (8 hex digits)

  • [0-9a-fA-F]{4}: Second group (4 hex digits)

  • [1-5][0-9a-fA-F]{3}: Third group (Version 1–5 UUID)

  • [89abAB][0-9a-fA-F]{3}: Fourth group (Variant)

  • [0-9a-fA-F]{12}: Final group (12 hex digits)


This regex matches all standard UUIDs (versions 1 through 5). If you want to strictly match version 4 UUIDs, you can use a more specific pattern:

// Regular expression (String form) for a Version 4 UUID (case-insensitive)
public static final String UUID_V4_STRING = 
    "[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}";

// Compiled Pattern for Version 4 UUIDs
public static final Pattern UUID_V4 = 
    Pattern.compile(UUID_V4_STRING, Pattern.CASE_INSENSITIVE);

Key differences:

  • The at the start of the third group ensures only version 4 UUIDs are matched.

  • The regex is case-insensitive to accommodate both lowercase and uppercase hexadecimal digits.


Java Code Example


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

public class UUIDValidator {
    public static void main(String[] args) {
        String uuid = "550e8400-e29b-41d4-a716-446655440000";
        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(uuid);

        if (matcher.matches()) {
            System.out.println("Valid UUID");
        } else {
            System.out.println("Invalid UUID");
        }
    }
}


Comprehensive UUID Regex Patterns in Java


For more flexibility, here are some common patterns you may encounter in Java projects:

// General UUID (any version, case-insensitive)
public static final String UUID_STRING = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"; 
public static final Pattern UUID = Pattern.compile(UUID_STRING);

// Version 4 UUID (random, case-insensitive)
public static final String UUID_V4_STRING = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}"; 
public static final Pattern UUID_V4 = Pattern.compile(UUID_V4_STRING);

Tip:

  • The first regex matches any valid UUID format (versions 1-5).

  • The second regex specifically targets version 4 UUIDs for scenarios where you need stricter validation.


Valid & Invalid UUIDs


Valid:

  • 123e4567-e89b-12d3-a456-426614174000

  • 550e8400-e29b-41d4-a716-446655440000


Invalid:

  • 123e4567e89b12d3a456426614174000 (missing hyphens)

  • zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz (invalid characters)

  • 123e4567-e89b-62d3-a456-426614174000 (invalid version: 6)


Common Use Cases

  • Database Keys: Use UUIDs as non-sequential primary keys for privacy.

  • Distributed Systems: Avoid key collisions across microservices or clusters.

  • API Resources: Identify and secure RESTful endpoints.

  • User IDs: Generate anonymous and unique identifiers for users.


Pro Tips

  • Use UUID version 4 (randomly generated) for security and simplicity.

  • Validate UUID format on both frontend and backend to prevent malformed input.

  • Normalize UUIDs by converting to lowercase before comparing or storing.

  • If you expect only certain versions (e.g., v4), refine the regex to match only those.

  • Store UUIDs as VARCHAR(36) in SQL databases to preserve formatting.

  • Regex checks format, not uniqueness — use a UUID library for generation.

By understanding and applying these regex patterns, you can easily adapt your UUID validation to fit the needs of your backend, API, or distributed system—whether you need to accept any UUID or specifically version 4 for enhanced randomness.


Regex Patterns for UUID Validation


When validating UUIDs in your Java code, you can choose how strict you want to be:

  • Generic UUID (any version):

    [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}

    This pattern matches any UUID, regardless of version.

  • Version 4 UUID only:

    [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}

    This stricter regex ensures the UUID is specifically version 4 (random), with the '4' in the third group and the correct variant in the fourth.


To implement these in Java, compile your pattern with and use it with as shown in the code example above. Adjust the pattern depending on whether you want to accept all UUIDs or only version 4.

Whitespace and capitalization don't matter to the regex, but it's best practice to store and compare UUIDs in lowercase for consistency.

If you need to validate UUIDs in other languages or systems, these patterns work in most environments that support Perl-compatible regular expressions.


Combine with These Tools

Frequently asked questions

Does this regex validate all UUID versions?×
It matches versions 1 to 5. For more precise control, modify the pattern to match a specific version only.
Are UUIDs case-sensitive?+
Can this be used for database keys?+
What happens if the UUID is missing hyphens?+
How do I generate a UUID in Java?+