Building AI Agent? Test & Secure your AI Agent nowRequest access
getting startedJava
UUID Regex Java Validator

UUID Regex Java Validator

Validate UUIDs in Java with ease using the UUID Regex Java Validator. Whether you’re working with databases, distributed systems, or backend services, this tool ensures your UUIDs follow correct formatting rules for reliable identification. Powered by Java’s java.util.regex, it’s ideal for testing patterns across APIs, logs, and data pipelines.


Looking to validate more fields in Java? Explore the:


UUID Regex Java Validator - Documentation

What is a UUID?

A UUID (or GUID) is a 128-bit identifier used to uniquely label data. It appears as a 36-character string in this format:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
  • M: Indicates UUID version (1–5)

  • N: Indicates the variant (typically 8, 9, A, or B in hex)


UUIDs are essential for unique identification across APIs, distributed systems, databases, and resource tagging.


Java Regex Pattern for UUID

To match standard UUIDs (versions 1–5):

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

  • [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} – Version (1–5)

  • [89abAB][0-9a-fA-F]{3} – Variant

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


To strictly validate version 4 UUIDs:

"[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}"



Java Implementation Example

To put this regex to work in Java, you can define it as a String or compile it as a Pattern for efficient reuse:

For validating specifically version 4 UUIDs, you might want to use a slightly stricter pattern:

These patterns ensure your UUID values are compliant with RFC 4122, catching common format mistakes before they hit your backend or database.


Version 4 UUID Pattern Breakdown

  • First group (8 hex digits)

  • Second group (4 hex digits)

  • Third group — the leading '4' specifically identifies it as version 4

  • Fourth group (variant, must begin with 8, 9, a, or b)

  • Final group (12 hex 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");
    }
}

}

Or, for strictly Version 4 UUIDs, use and the compiled as shown above.

Reusable Java UUID Patterns

// Generic UUID (any version)
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, Pattern.CASE_INSENSITIVE);

// Version 4 UUID only 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, Pattern.CASE_INSENSITIVE);


Valid UUID Examples

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

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


Invalid UUID Examples

  • 123e4567e89b12d3a456426614174000 (missing hyphens)

  • zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz (non-hex characters)

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


Common Use Cases

  • Database Keys – Use UUIDs as non-sequential primary keys

  • Microservices – Avoid key collisions in distributed systems

  • API Resources – Identify RESTful endpoints and resources

  • User IDs – Generate anonymous, secure user identifiers


Pro Tips

  • Prefer Version 4 UUIDs: Use UUID version 4 (randomly generated) for security and simplicity.

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

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

  • Be Specific When Needed: If you expect only certain versions (e.g., v4), refine the regex to match only those using the version-specific pattern above.

By leveraging these patterns—and understanding the subtle differences between general and version-specific UUID regex—you can ensure your Java applications handle identifier validation robustly and efficiently.


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?

No. Both uppercase and lowercase hexadecimal characters are accepted.

Can this be used for database keys?

Yes, UUIDs are ideal for primary keys, especially in distributed systems.

What happens if the UUID is missing hyphens?

The regex will fail validation — proper format with hyphens is required.

How do I generate a UUID in Java?

Use UUID.randomUUID().toString() to create a compliant version 4 UUID.

Test your APIs today!

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