UUID Regex Python Validator

Search...

⌘K

UUID Regex Python Validator

Search...

⌘K


UUID Regex Python Validator

Validate UUID strings with the UUID Regex Python Validator built for Python developers working on secure APIs, databases, and distributed systems. This tool checks whether your identifiers conform to the standard UUID format using Python regex. For full validation suites, pair it with the Email Regex Python Validator or IP Address Regex Python Validator to strengthen your backend input checks.

550e8400-e29b-41d4-a716-446655440000
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 UUID Regex?


A UUID (Universally Unique Identifier) is a 128-bit identifier used to uniquely label data across systems. It’s typically represented as 32 hexadecimal characters split into 5 groups by hyphens, like:


123e4567-e89b-12d3-a456-426614174000

UUIDs are used in databases, APIs, microservices, IoT, and any context requiring global uniqueness.


UUID Regex Pattern in Python


To match a standard UUID format (v1–v5), use this regex pattern:


^[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 pattern checks:

  • 8 hexadecimal characters

  • 4-digit version group (starting with 1–5)

  • 4-digit variant group (starting with 8–b)

  • Final 12 hex digits


How to Validate UUIDs Using Regex in Python


Here’s how to use the re module in Python to match valid UUIDs:


import re

def is_valid_uuid(uuid_str):
    pattern = re.compile(r'^[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}$')
    return bool(pattern.match(uuid_str))

# Example test
uuids = [
    "123e4567-e89b-12d3-a456-426614174000",  # Valid
    "123e4567e89b12d3a456426614174000",      # Invalid (no hyphens)
    "ZZZe4567-e89b-12d3-a456-426614174000"   # Invalid (non-hex)
]

for u in uuids:
    print(f"{u} -> {is_valid_uuid(u)}")


The above function, , provides a straightforward way to check if a string matches the standard UUID regex pattern. It returns if the input matches the expected UUID format; otherwise, it returns .

For a more robust validation—especially when you want to ensure the string actually represents a valid UUID object in Python (not just the right pattern)—you can also use the built-in module. This approach attempts to create a object from the input. If successful, the input is a valid UUID; if not, a is raised and caught, indicating an invalid UUID:

import uuid

def is_valid_uuid_strict(value):
    try:
        uuid.UUID(value)
        return True
    except ValueError:
        return False
  • Use the regex-based is_valid_uuid for simple format checks.

  • Use the module method for deeper validation of actual UUID values.

Both methods are useful, depending on whether you need a quick format check or a full validation that catches subtle issues outside the pattern itself.


Why Convert Inputs to Strings Before Validation?

Before you run your UUID through the validator, it’s a good idea to convert your input to a string. This helps you avoid errors when someone passes in a value that isn’t already in string form—like an integer, a UUID object, or even something unexpected.

By wrapping your input in str(value) before the check, you make sure the validator always receives a string. This simple habit can save you from confusing bugs, especially when your code might encounter different data types (for example, when handling JSON, user input, or data from external APIs). It’s a defensive move that keeps your validation logic robust across a variety of situations.


Handling Mixed Input Types for UUID Validation

Sometimes, the value you want to check isn't a string—it might be an integer, an object, or even something else unexpected (Python keeps us on our toes like that). To tackle this, it’s a good idea to first convert whatever you receive to a string before running your UUID regex validation. That way, the function won’t throw a fit if it sees an integer or another type.

Here’s how you might tweak the earlier function to handle mixed input types:

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

With this modification, you can pass in integers, strings, or other types—and as long as their string representation matches the UUID format, you’re good. This approach keeps your validation robust and friendly to all sorts of input.


Matching UUIDs Without Hyphens

If you need to match UUIDs where hyphens have been removed—sometimes encountered in compact database keys or legacy systems—you can simply adjust the regex pattern. Instead of including hyphens as separators, look for a continuous string of 32 hexadecimal characters.

Here's how you can modify the pattern:

^[0-9a-fA-F]

This pattern matches UUIDs written as a single, uninterrupted 32-character hexadecimal string (e.g., 123e4567e89b12d3a456426614174000). It’s useful when handling non-standard formats or cleaning up user input before validation.

Be sure to pick the pattern that matches your data’s format—hyphenated or plain—depending on your application’s needs.


Handling Non-String UUID Inputs

The uuid.UUID constructor in Python is quite flexible—it can accept more than just string input. While most commonly used with string representations of UUIDs, you might sometimes encounter integers or other types when parsing data.

To ensure smooth validation, it’s a good idea to explicitly convert your input to a string before passing it to uuid.UUID. This prevents unexpected errors from occurring when dealing with numbers or mixed input types. For example:

import uuid

value = 123456789123456789123456789123456789
uuid_obj = uuid.UUID(str(value))

By converting inputs to strings first, you make your UUID validation more robust, especially when the data’s origin or type might vary. This small step saves debugging headaches, particularly when your application interfaces with external systems or users.


Use Cases

  • API Resource Identification: Use UUIDs in REST endpoints or JSON payloads to reference records uniquely.

  • Database Indexing: Use UUIDs as primary keys to prevent collision across distributed tables.

  • Session Management: Validate UUID-based tokens for user sessions in secure applications.

  • Form Submissions: Check if hidden fields in forms pass valid UUIDs.

  • Combine with Password Regex Python Validator to secure user login data.


Pro Tips

  • UUIDs can be lowercase or uppercase, and this pattern matches both.

  • Use Python’s uuid module to generate and compare UUIDs, and validate format with this regex.

  • Alternatively, you can validate whether a string is a true UUID (not just matching the format, but actually constructible as a UUID object) by attempting to create a instance in a try/except block. If the string is a valid UUID, the object is created successfully and you return ; if not, a is raised and you return . This adds an extra layer of assurance beyond regex format matching, making your validation more robust.

  • This regex only checks the format, not the UUID’s randomness or version logic.

  • Normalize UUIDs to lowercase using .lower() before storage for consistency.

  • Combine with the Email Regex Python Validator or Phone Number Regex Python Validator for full form validation in Python apps.


Frequently asked questions

What UUID versions does this regex support?×
This pattern supports UUID versions 1 to 5 by checking the version digit.
Can I use this regex to validate UUIDs in uppercase?+
Is this format suitable for database keys?+
What happens if a UUID has no hyphens?+
Can this regex detect invalid UUID version numbers?+
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?+

UUID Regex Python Validator

Search...

⌘K

UUID Regex Python Validator

Search...

⌘K


UUID Regex Python Validator

UUID Regex Python Validator

Validate UUID strings with the UUID Regex Python Validator built for Python developers working on secure APIs, databases, and distributed systems. This tool checks whether your identifiers conform to the standard UUID format using Python regex. For full validation suites, pair it with the Email Regex Python Validator or IP Address Regex Python Validator to strengthen your backend input checks.

550e8400-e29b-41d4-a716-446655440000
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.

UUID Regex Python Validator - Documentation

What is UUID Regex?


A UUID (Universally Unique Identifier) is a 128-bit identifier used to uniquely label data across systems. It’s typically represented as 32 hexadecimal characters split into 5 groups by hyphens, like:


123e4567-e89b-12d3-a456-426614174000

UUIDs are used in databases, APIs, microservices, IoT, and any context requiring global uniqueness.


UUID Regex Pattern in Python


To match a standard UUID format (v1–v5), use this regex pattern:


^[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 pattern checks:

  • 8 hexadecimal characters

  • 4-digit version group (starting with 1–5)

  • 4-digit variant group (starting with 8–b)

  • Final 12 hex digits


How to Validate UUIDs Using Regex in Python


Here’s how to use the re module in Python to match valid UUIDs:


import re

def is_valid_uuid(uuid_str):
    pattern = re.compile(r'^[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}$')
    return bool(pattern.match(uuid_str))

# Example test
uuids = [
    "123e4567-e89b-12d3-a456-426614174000",  # Valid
    "123e4567e89b12d3a456426614174000",      # Invalid (no hyphens)
    "ZZZe4567-e89b-12d3-a456-426614174000"   # Invalid (non-hex)
]

for u in uuids:
    print(f"{u} -> {is_valid_uuid(u)}")


The above function, , provides a straightforward way to check if a string matches the standard UUID regex pattern. It returns if the input matches the expected UUID format; otherwise, it returns .

For a more robust validation—especially when you want to ensure the string actually represents a valid UUID object in Python (not just the right pattern)—you can also use the built-in module. This approach attempts to create a object from the input. If successful, the input is a valid UUID; if not, a is raised and caught, indicating an invalid UUID:

import uuid

def is_valid_uuid_strict(value):
    try:
        uuid.UUID(value)
        return True
    except ValueError:
        return False
  • Use the regex-based is_valid_uuid for simple format checks.

  • Use the module method for deeper validation of actual UUID values.

Both methods are useful, depending on whether you need a quick format check or a full validation that catches subtle issues outside the pattern itself.


Why Convert Inputs to Strings Before Validation?

Before you run your UUID through the validator, it’s a good idea to convert your input to a string. This helps you avoid errors when someone passes in a value that isn’t already in string form—like an integer, a UUID object, or even something unexpected.

By wrapping your input in str(value) before the check, you make sure the validator always receives a string. This simple habit can save you from confusing bugs, especially when your code might encounter different data types (for example, when handling JSON, user input, or data from external APIs). It’s a defensive move that keeps your validation logic robust across a variety of situations.


Handling Mixed Input Types for UUID Validation

Sometimes, the value you want to check isn't a string—it might be an integer, an object, or even something else unexpected (Python keeps us on our toes like that). To tackle this, it’s a good idea to first convert whatever you receive to a string before running your UUID regex validation. That way, the function won’t throw a fit if it sees an integer or another type.

Here’s how you might tweak the earlier function to handle mixed input types:

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

With this modification, you can pass in integers, strings, or other types—and as long as their string representation matches the UUID format, you’re good. This approach keeps your validation robust and friendly to all sorts of input.


Matching UUIDs Without Hyphens

If you need to match UUIDs where hyphens have been removed—sometimes encountered in compact database keys or legacy systems—you can simply adjust the regex pattern. Instead of including hyphens as separators, look for a continuous string of 32 hexadecimal characters.

Here's how you can modify the pattern:

^[0-9a-fA-F]

This pattern matches UUIDs written as a single, uninterrupted 32-character hexadecimal string (e.g., 123e4567e89b12d3a456426614174000). It’s useful when handling non-standard formats or cleaning up user input before validation.

Be sure to pick the pattern that matches your data’s format—hyphenated or plain—depending on your application’s needs.


Handling Non-String UUID Inputs

The uuid.UUID constructor in Python is quite flexible—it can accept more than just string input. While most commonly used with string representations of UUIDs, you might sometimes encounter integers or other types when parsing data.

To ensure smooth validation, it’s a good idea to explicitly convert your input to a string before passing it to uuid.UUID. This prevents unexpected errors from occurring when dealing with numbers or mixed input types. For example:

import uuid

value = 123456789123456789123456789123456789
uuid_obj = uuid.UUID(str(value))

By converting inputs to strings first, you make your UUID validation more robust, especially when the data’s origin or type might vary. This small step saves debugging headaches, particularly when your application interfaces with external systems or users.


Use Cases

  • API Resource Identification: Use UUIDs in REST endpoints or JSON payloads to reference records uniquely.

  • Database Indexing: Use UUIDs as primary keys to prevent collision across distributed tables.

  • Session Management: Validate UUID-based tokens for user sessions in secure applications.

  • Form Submissions: Check if hidden fields in forms pass valid UUIDs.

  • Combine with Password Regex Python Validator to secure user login data.


Pro Tips

  • UUIDs can be lowercase or uppercase, and this pattern matches both.

  • Use Python’s uuid module to generate and compare UUIDs, and validate format with this regex.

  • Alternatively, you can validate whether a string is a true UUID (not just matching the format, but actually constructible as a UUID object) by attempting to create a instance in a try/except block. If the string is a valid UUID, the object is created successfully and you return ; if not, a is raised and you return . This adds an extra layer of assurance beyond regex format matching, making your validation more robust.

  • This regex only checks the format, not the UUID’s randomness or version logic.

  • Normalize UUIDs to lowercase using .lower() before storage for consistency.

  • Combine with the Email Regex Python Validator or Phone Number Regex Python Validator for full form validation in Python apps.


Frequently asked questions

What UUID versions does this regex support?×
This pattern supports UUID versions 1 to 5 by checking the version digit.
Can I use this regex to validate UUIDs in uppercase?+
Is this format suitable for database keys?+
What happens if a UUID has no hyphens?+
Can this regex detect invalid UUID version numbers?+