DD
DevDash

Regex for Docker Container ID

Regex Pattern

^[0-9a-f]{12}(?:[0-9a-f]{52})?$

Docker container ID (short 12 or full 64 hex chars)

Quick Answer

The regex pattern for docker container id is `^[0-9a-f]{12}(?:[0-9a-f]{52})?$`. Docker container ID (short 12 or full 64 hex chars). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
a1b2c3d4e5f6✓ Matches
abc123def456✓ Matches
a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6abcd✓ Matches
a1b2c3d4e5✗ No match
A1B2C3D4E5F6✗ No match
not-hex-chars✗ No match

Code Examples

javascript

const regex = /^[0-9a-f]{12}(?:[0-9a-f]{52})?$/;
const isValid = regex.test(value);

python

import re
pattern = r'^[0-9a-f]{12}(?:[0-9a-f]{52})?$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^[0-9a-f]{12}(?:[0-9a-f]{52})?$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^[0-9a-f]{12}(?:[0-9a-f]{52})?$/', $value)) {
    echo "valid";
}

java

String pattern = "^[0-9a-f]{12}(?:[0-9a-f]{52})?$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.