UUID regex PHP

UUID

UUID is a 128-bit label used for identifications in computer systems.

UUID regex

Below is a simple UUID regular expression that will work most of the time:

"/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/"

Test it!
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i

True

False

Enter a text in the input above to see the result

Example code in PHP:

// Validate if a string is a valid uuid
$uuid_validation_regex = "/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/"; 
echo preg_match($uuid_validation_regex, '123e4567-e89b-12d3-a456-426614174000'); // returns 1

// Extract uuid from a string
$extract_uuid_pattern = "/[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}/";
$string_to_match = 'The UUID 123e4567-e89b-12d3-a456-426614174000 for node Node 01 is not unique.';
preg_match_all($extract_uuid_pattern, $string_to_match, $matches);
print_r($matches[0])// matches[0] is ['123e4567-e89b-12d3-a456-426614174000']

Test it!

True

False

Enter a text in the input above to see the result

Test it!

True

False

Enter a text in the input above to see the result