REGEXVAULTv2.0
Dev & Systems/Docker
Verified Safe

Docker Image Full Reference Regex for Java

/^(?:(?:[a-zA-Z0-9][a-zA-Z0-9\-._]*(?::[0-9]+)?)/)?(?:[a-z][a-z0-9\-._]*/)?[a-z][a-z0-9\-._]*(?::([a-zA-Z0-9][a-zA-Z0-9._\-]{0,127})|@(sha256:[a-fA-F0-9]{64}))?$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching docker image full reference, ported and verified for Java. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

Java
// Docker Image Full Reference
// ReDoS-safe | RegexVault — Dev & Systems > Docker

import java.util.regex.Pattern;

public class DockerImageFullReferenceValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^(?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-._]*(?::[0-9]+)?)/)?(?:[a-z][a-z0-9\\-._]*/)?[a-z][a-z0-9\\-._]*(?::([a-zA-Z0-9][a-zA-Z0-9._\\-]{0,127})|@(sha256:[a-fA-F0-9]{64}))?$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("nginx")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
nginxNginx:latest
nginx:latestnginx: latest
ubuntu:22.04nginx:tag with space
library/nginx:alpineregistry/Image:tag
myregistry.com:5000/myimage:v1.0
nginx@sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4

When to use this pattern

This pattern is drawn from the Dev & Systems > Docker category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

A registry hostname must contain a period or colon or be 'localhost' to distinguish it from a namespace — 'myregistry/image' is parsed as namespace/image on Docker Hub.

Technical Notes

Group 1 = tag, group 2 = digest. Docker resolves bare names like 'nginx' to 'docker.io/library/nginx:latest'. Always specify the full reference in IaC and CI/CD pipelines.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern