GUID Regex Python Validator

Search...

⌘K

GUID Regex Python Validator

Search...

⌘K


GUID Regex Python Validator

The GUID Regex Python Validator is designed to help developers and testers confirm that GUIDs (Globally Unique Identifiers) match proper formatting. Perfect for validating API tokens, resource IDs, or database keys, this tool is essential for quality checks. You can pair it with the UUID Regex Python Validator or the Mac Address Regex Python Validator to build solid validation flows in your Python projects.

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
Test your APIs today!

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

Regular Expression - Documentation

What Is a GUID?


A GUID is a 128-bit unique identifier often used to label resources like users, sessions, or records. It appears as a string in this format:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Where each x is a hexadecimal digit (0-9, a-f, or A-F). While GUIDs and UUIDs are functionally similar, GUIDs are more common in Microsoft-based systems.


Regex Pattern for GUID in Python


Here’s a regex pattern that accurately validates a standard 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}$


This ensures:

  • 8 digits at the beginning

  • Followed by three groups of 4 digits (hyphen-separated)

  • Ends with 12 hex digits


Improving the Regex for Stricter GUID Validation


The original pattern, \w{8}-\w{4}-\w{4}-\w{4}-\w{12}, does check for the correct overall structure, but it’s a bit too relaxed—it matches any “word” character (letters, numbers, and underscores), not just hexadecimal digits. That means non-hex characters (like underscores) would incorrectly pass as valid.

To tighten up the validation and ensure each segment matches only hexadecimal characters (0-9, a-f, A-F), you can refine the pattern like so:

^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]

This version ensures:

  • Every group contains only valid hex digits.

  • The exact lengths and hyphen placements are enforced.

  • Extra or invalid characters will be correctly rejected.

If your use case demands even stricter validation (like validating the specific version or variant bits of the GUID, as seen in standards-compliant UUIDs), you can take things further:

^[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]

With these improvements, your regex pattern will robustly validate GUIDs with both structure and content requirements.


Python Example to Validate GUID


import re

def is_valid_guid(guid):
    pattern = re.compile(
        r'^[0-9a-fA-F]{8}-'
        r'[0-9a-fA-F]{4}-'
        r'[1-5][0-9a-fA-F]{3}-'
        r'[89abAB][0-9a-fA-F]{3}-'
        r'[0-9a-fA-F]{12}$'
    )
    return bool(pattern.fullmatch(guid))

# Test GUIDs
test_guids = [
    "123e4567-e89b-12d3-a456-426614174000",
    "00112233-4455-6677-8899-aabbccddeeff",
    "invalid-guid-format"
]

for g in test_guids:
    print(f"{g} -> {is_valid_guid(g)}")


Should You Use \w or [a-f0-9] in GUID Regex Patterns?


When it comes to validating GUIDs in Python, the regex you choose matters. While \w is a handy shorthand for "word character" (which includes letters, digits, and underscores), it's actually a bit too generous for GUID validation.

The Core Difference

  • \w matches uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore (_).

  • [a-f0-9] specifically restricts the match to lowercase hexadecimal digits—which is exactly what standard GUIDs use.

Why Precision Matters

Suppose you use the common pattern:

This will accept _ and any letter in the alphabet, not just the hexadecimal set. That means invalid GUIDs could sneak through, especially strings with underscores or non-hex characters.

Contrast that with:

^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]

This enforces only lowercase hex digits (a-f, 0-9) at each segment, blocking accidental or malicious input that doesn’t match the GUID format.

When to Use Which

  • Choose [a-f0-9] if you want strict, lowercase-only GUID validation (as seen in many APIs and database schemas).

  • If you expect uppercase hex in GUIDs, use [a-fA-F0-9].

Remember: \w = too broad for GUIDs. Use explicit character classes for accurate validation.


Why \w Isn’t Enough


While it might be tempting to use a pattern like \w{8}-\w{4}-\w{4}-\w{4}-\w{12} when hunting for GUIDs, this shortcut comes with a catch. The \w character class matches any “word” character—which includes not just hexadecimal digits, but also underscores and all uppercase and lowercase letters (A-Z, a-z, 0-9, and _).

This means:

  • Underscores or non-hex letters can sneak through—turning what looks like a GUID into something the parser won’t recognize.

  • For example, a string like ABCD_EF9-1234-5678-9abc-def012345678 would (incorrectly) match your regex, even though underscores aren’t valid in standard GUIDs.

  • Adding extra characters, or swapping in non-hex values, could still produce false positives.

The more restrictive pattern provided above explicitly limits each character to only valid hexadecimal digits (0-9, a-f, A-F), ensuring that only proper GUIDs pass your validation check. If you want to mirror what you’ll find in real-world GUIDs from Python libraries, or databases like MongoDB or PostgreSQL, this stricter approach is the way to go.


Use Cases


  • Database Record IDs: Use GUIDs as primary keys for distributed systems.

  • Authentication Tokens: Validate GUIDs passed via API headers or query strings.

  • Data Synchronization: Match GUIDs in local and remote storage to ensure consistency.

  • Debugging Tools: Use it with the IP Address Regex Python Validator to monitor network-based identifiers.


Pro Tips


  • Always sanitize GUID inputs—especially from client-side sources.

  • Strip leading/trailing whitespaces before validation.

  • Combine it with the Date Regex Python Validator when working with time-stamped identifiers.

  • Use lowercase GUIDs in systems where case doesn’t matter to maintain consistency.


Frequently asked questions

What’s the difference between a GUID and a UUID?×
Functionally, they are similar. UUID is the official standard, while GUID is Microsoft’s term for the same thing.
Can this regex detect invalid or duplicate GUIDs?+
What versions of GUID does this support?+
Does case matter in GUIDs?+
Is this suitable for database validation?+
Can I import Figma designs?+
Is it SEO-friendly?+
Can I collaborate with my team?+
Is hosting included?+
Can I export code?+
Is there a free plan?+
Can I use custom fonts?+

GUID Regex Python Validator

Search...

⌘K

GUID Regex Python Validator

Search...

⌘K


GUID Regex Python Validator

GUID Regex Python Validator

The GUID Regex Python Validator is designed to help developers and testers confirm that GUIDs (Globally Unique Identifiers) match proper formatting. Perfect for validating API tokens, resource IDs, or database keys, this tool is essential for quality checks. You can pair it with the UUID Regex Python Validator or the Mac Address Regex Python Validator to build solid validation flows in your Python projects.

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
Test your APIs today!

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

Regular Expression - Documentation

What Is a GUID?


A GUID is a 128-bit unique identifier often used to label resources like users, sessions, or records. It appears as a string in this format:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Where each x is a hexadecimal digit (0-9, a-f, or A-F). While GUIDs and UUIDs are functionally similar, GUIDs are more common in Microsoft-based systems.


Regex Pattern for GUID in Python


Here’s a regex pattern that accurately validates a standard 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}$


This ensures:

  • 8 digits at the beginning

  • Followed by three groups of 4 digits (hyphen-separated)

  • Ends with 12 hex digits


Improving the Regex for Stricter GUID Validation


The original pattern, \w{8}-\w{4}-\w{4}-\w{4}-\w{12}, does check for the correct overall structure, but it’s a bit too relaxed—it matches any “word” character (letters, numbers, and underscores), not just hexadecimal digits. That means non-hex characters (like underscores) would incorrectly pass as valid.

To tighten up the validation and ensure each segment matches only hexadecimal characters (0-9, a-f, A-F), you can refine the pattern like so:

^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]

This version ensures:

  • Every group contains only valid hex digits.

  • The exact lengths and hyphen placements are enforced.

  • Extra or invalid characters will be correctly rejected.

If your use case demands even stricter validation (like validating the specific version or variant bits of the GUID, as seen in standards-compliant UUIDs), you can take things further:

^[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]

With these improvements, your regex pattern will robustly validate GUIDs with both structure and content requirements.


Python Example to Validate GUID


import re

def is_valid_guid(guid):
    pattern = re.compile(
        r'^[0-9a-fA-F]{8}-'
        r'[0-9a-fA-F]{4}-'
        r'[1-5][0-9a-fA-F]{3}-'
        r'[89abAB][0-9a-fA-F]{3}-'
        r'[0-9a-fA-F]{12}$'
    )
    return bool(pattern.fullmatch(guid))

# Test GUIDs
test_guids = [
    "123e4567-e89b-12d3-a456-426614174000",
    "00112233-4455-6677-8899-aabbccddeeff",
    "invalid-guid-format"
]

for g in test_guids:
    print(f"{g} -> {is_valid_guid(g)}")


Should You Use \w or [a-f0-9] in GUID Regex Patterns?


When it comes to validating GUIDs in Python, the regex you choose matters. While \w is a handy shorthand for "word character" (which includes letters, digits, and underscores), it's actually a bit too generous for GUID validation.

The Core Difference

  • \w matches uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore (_).

  • [a-f0-9] specifically restricts the match to lowercase hexadecimal digits—which is exactly what standard GUIDs use.

Why Precision Matters

Suppose you use the common pattern:

This will accept _ and any letter in the alphabet, not just the hexadecimal set. That means invalid GUIDs could sneak through, especially strings with underscores or non-hex characters.

Contrast that with:

^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]

This enforces only lowercase hex digits (a-f, 0-9) at each segment, blocking accidental or malicious input that doesn’t match the GUID format.

When to Use Which

  • Choose [a-f0-9] if you want strict, lowercase-only GUID validation (as seen in many APIs and database schemas).

  • If you expect uppercase hex in GUIDs, use [a-fA-F0-9].

Remember: \w = too broad for GUIDs. Use explicit character classes for accurate validation.


Why \w Isn’t Enough


While it might be tempting to use a pattern like \w{8}-\w{4}-\w{4}-\w{4}-\w{12} when hunting for GUIDs, this shortcut comes with a catch. The \w character class matches any “word” character—which includes not just hexadecimal digits, but also underscores and all uppercase and lowercase letters (A-Z, a-z, 0-9, and _).

This means:

  • Underscores or non-hex letters can sneak through—turning what looks like a GUID into something the parser won’t recognize.

  • For example, a string like ABCD_EF9-1234-5678-9abc-def012345678 would (incorrectly) match your regex, even though underscores aren’t valid in standard GUIDs.

  • Adding extra characters, or swapping in non-hex values, could still produce false positives.

The more restrictive pattern provided above explicitly limits each character to only valid hexadecimal digits (0-9, a-f, A-F), ensuring that only proper GUIDs pass your validation check. If you want to mirror what you’ll find in real-world GUIDs from Python libraries, or databases like MongoDB or PostgreSQL, this stricter approach is the way to go.


Use Cases


  • Database Record IDs: Use GUIDs as primary keys for distributed systems.

  • Authentication Tokens: Validate GUIDs passed via API headers or query strings.

  • Data Synchronization: Match GUIDs in local and remote storage to ensure consistency.

  • Debugging Tools: Use it with the IP Address Regex Python Validator to monitor network-based identifiers.


Pro Tips


  • Always sanitize GUID inputs—especially from client-side sources.

  • Strip leading/trailing whitespaces before validation.

  • Combine it with the Date Regex Python Validator when working with time-stamped identifiers.

  • Use lowercase GUIDs in systems where case doesn’t matter to maintain consistency.


Frequently asked questions

What’s the difference between a GUID and a UUID?×
Functionally, they are similar. UUID is the official standard, while GUID is Microsoft’s term for the same thing.
Can this regex detect invalid or duplicate GUIDs?+
What versions of GUID does this support?+
Does case matter in GUIDs?+
Is this suitable for database validation?+