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)


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");
        }
    }
}


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.


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?+