GUID Regex Java Validator

Search...

⌘K

GUID Regex Java Validator

Search...

⌘K


GUID Regex Java Validator

GUID Regex Java Validator

The GUID Regex Java Validator helps developers confirm whether a GUID (Globally Unique Identifier) matches the correct syntax using Java regex. This is particularly useful for systems where unique object IDs, session tokens, or API keys are involved.

Explore related Java tools for data validation and encoding:


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

What is a GUID in Java?

A GUID (or UUID) is a 128-bit number used to uniquely identify data in systems. It is typically formatted as:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx


Where:

  • x is a hexadecimal digit

  • M indicates the version

  • N indicates the variant


A GUID helps ensure global uniqueness in distributed systems, databases, or API transactions.


Java Regex Pattern for GUID

The commonly used regex pattern for validating GUIDs:

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


What it matches:

  • 8 hexadecimal digits

  • A hyphen

  • 4 hexadecimal digits

  • A hyphen

  • 4 hexadecimal digits starting with version 1–5

  • A hyphen

  • 4 hexadecimal digits starting with 8, 9, A, or B (variant)

  • A hyphen

  • 12 hexadecimal digits


Java Code Example

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

public class GUIDValidator {
    public static void main(String[] args) {
        String input = "3f2504e0-4f89-11d3-9a0c-0305e82c3301";

        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(input);

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


Sample Inputs


Valid GUIDs:

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

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

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


Invalid GUIDs:

  • 3f2504e0-4f89-11d3-9a0c0305e82c3301 (missing hyphen)

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

  • 12345 (too short)


Pro Tips

  • Always validate GUIDs before storing or using them in APIs or sessions.

  • GUIDs are case-insensitive. Your regex should allow both uppercase and lowercase letters (use a-fA-F).

  • Remove whitespace or invisible characters before running regex checks.

  • If you’re generating GUIDs in Java, use UUID.randomUUID().toString() for guaranteed format compliance.

  • For stricter validation, create version-specific patterns (e.g., only v4 UUIDs).

  • Never expose internal or sensitive GUIDs directly — hash or encode them with Hash Generator Java or Base64 Encoder.


Use Cases

  • API Key Validation: Ensure passed tokens follow GUID structure.

  • Database Keys: Confirm format of primary or foreign keys.

  • Logging Systems: Clean and validate UUID-based log identifiers.

  • Form Submissions: Accept only properly formatted GUIDs in front-end fields.


Combine with These Tools

Frequently asked questions

Are GUIDs and UUIDs the same?×
Yes, they are often used interchangeably. GUID is more common in Microsoft systems; UUID is the official term per RFC 4122.
Does this regex check if a GUID is unique?+
Can I validate upper or lowercase GUIDs?+
Does this work for version 4 GUIDs?+
What if my GUIDs are generated without hyphens?+