Code
UUID version 4 Regex
Version 4 UUIDs are random identifiers used as database keys, request IDs and file names. Their canonical text form is 32 hex digits split 8-4-4-4-12 by hyphens. The first digit of the third group is always 4, and the first digit of the fourth group is 8, 9, a or b to mark the standard variant. This pattern checks all of that, case-insensitively. It is ideal for route parameters, API input checks and log parsing. It deliberately rejects other UUID versions, such as time-based v1 or sortable v7, and it cannot tell you whether the value was really generated randomly.
Pattern (JavaScript)
/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/iStarts at the start of the line/string, then one of the characters \da-f, exactly 8 times, then the character "-", then one of the characters \da-f, exactly 4 times, then the text "-4", then one of the characters \da-f, exactly 3 times, then the character "-", then one of the characters 89ab, then one of the characters \da-f, exactly 3 times, then the character "-", then one of the characters \da-f, exactly 12 times, then the end of the line/string.
Examples
Matches
- ✓ 123e4567-e89b-42d3-a456-426614174000
- ✓ F47AC10B-58CC-4372-A567-0E02B2C3D479
- ✓ 00000000-0000-4000-8000-000000000000
Does not match
- ✗ 123e4567-e89b-12d3-a456-426614174000
- ✗ f47ac10b-58cc-4372-c567-0e02b2c3d479
- ✗ f47ac10b58cc4372a5670e02b2c3d479
- ✗ {f47ac10b-58cc-4372-a567-0e02b2c3d479}
Try it
0 matches (JavaScript engine)
123e4567-e89b-42d3-a456-426614174000
F47AC10B-58CC-4372-A567-0E02B2C3D479
00000000-0000-4000-8000-000000000000
123e4567-e89b-12d3-a456-426614174000
f47ac10b-58cc-4372-c567-0e02b2c3d479
f47ac10b58cc4372a5670e02b2c3d479
{f47ac10b-58cc-4372-a567-0e02b2c3d479}Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/gi;
const text = "123e4567-e89b-42d3-a456-426614174000";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$', re.I)
text = '123e4567-e89b-42d3-a456-426614174000'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i';
$text = '123e4567-e89b-42d3-a456-426614174000';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[\\da-f]{8}-[\\da-f]{4}-4[\\da-f]{3}-[89ab][\\da-f]{3}-[\\da-f]{12}$", Pattern.CASE_INSENSITIVE);
String text = "123e4567-e89b-42d3-a456-426614174000";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$`)
text := "123e4567-e89b-42d3-a456-426614174000"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$", RegexOptions.IgnoreCase);
var text = "123e4567-e89b-42d3-a456-426614174000";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"(?i)^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$").unwrap();
let text = "123e4567-e89b-42d3-a456-426614174000";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$/i
text = '123e4567-e89b-42d3-a456-426614174000'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why must the third group start with 4?
- That digit is the UUID version number. Version 4 means the identifier is random, while 1 is time-based and 7 is time-ordered. Checking it ensures you only accept v4 values.
- What does the [89ab] digit mean?
- It encodes the variant, which says how the rest of the bits are laid out. Standard RFC 4122 and RFC 9562 UUIDs use a first digit of 8, 9, a or b in the fourth group.
- How do I accept any UUID version?
- Replace the 4 in the third group with [1-8] and keep the variant check, or use [\da-f] for both if you need to accept non-standard values as well.