Go (RE2)

Docker Image Tag in GO

Match Docker image references with an explicit tag — e.g. nginx:1.21, mycorp/service:v2.3.1.

Try it in the GO tester →

Pattern

regexGO
\b[a-z0-9]+(?:[._\-\/][a-z0-9]+)*:[\w.\-]+\b   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\b[a-z0-9]+(?:[._\-\/][a-z0-9]+)*:[\w.\-]+\b`)
	input := `FROM nginx:1.21`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

Image name is lowercase alphanumerics with optional . _ - / separators for registry paths. Tag follows the colon and can include letters, digits, dots, hyphens, and underscores.

Examples

Input

FROM nginx:1.21

Matches

  • nginx:1.21

Input

image: mycorp/service:v2.3.1

Matches

  • mycorp/service:v2.3.1

Input

ubuntu:latest

Matches

  • ubuntu:latest

Same pattern, other engines

← Back to Docker Image Tag overview (all engines)