Dockerfile ENV Instruction in GO
Match Dockerfile `ENV KEY=value` (or `ENV KEY value`) instructions, capturing key and value.
Try it in the GO tester →Pattern
regexGO
^ENV\s+([A-Z_][A-Z0-9_]*)(?:[=\s]+)(.+)$ (flags: gm)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?m)^ENV\s+([A-Z_][A-Z0-9_]*)(?:[=\s]+)(.+)$`)
input := `ENV NODE_VERSION=20.10.0\nENV PORT 3000`
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
^ENV anchors to the line start (m flag enables per-line). \s+ requires whitespace. ([A-Z_][A-Z0-9_]*) captures the key (uppercase + underscores by convention). [=\s]+ matches either the modern `=` separator or the legacy whitespace separator. (.+)$ captures the rest of the line as the value.
Examples
Input
ENV NODE_VERSION=20.10.0\nENV PORT 3000Matches
ENV NODE_VERSION=20.10.0ENV PORT 3000
Input
ENV DATABASE_URL=postgres://localhost/dbMatches
ENV DATABASE_URL=postgres://localhost/db
Input
FROM node:20No match
—