File & Pathflags: gm
Dockerfile ENV Instruction
Match Dockerfile `ENV KEY=value` (or `ENV KEY value`) instructions, capturing key and value.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^ENV\\s+([A-Z_][A-Z0-9_]*)(?:[=\\s]+)(.+)$", "gm");
const input = "ENV NODE_VERSION=20.10.0\\nENV PORT 3000";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"^ENV\s+([A-Z_][A-Z0-9_]*)(?:[=\s]+)(.+)$", re.MULTILINE)
input_text = "ENV NODE_VERSION=20.10.0\nENV PORT 3000"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
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.
Pattern
regexengine-agnostic
^ENV\s+([A-Z_][A-Z0-9_]*)(?:[=\s]+)(.+)$ (flags: gm)Raw source: ^ENV\s+([A-Z_][A-Z0-9_]*)(?:[=\s]+)(.+)$
How it 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
—Common use cases
- •Dockerfile linting (hadolint-style rules)
- •Multi-stage build env extraction
- •Migrating from legacy ENV syntax to KEY=VALUE form
- •Detecting hardcoded secrets in container images
Related patterns
.env File Key-Value Line
File & PathParse KEY=value lines from .env config files, handling quoted values and trailing comments.
Hidden File (Dotfile)
File & PathMatches Unix-style hidden files (dotfiles) like .gitignore or .env.
File Extension
File & PathCaptures the file extension from a filename.
Image File Extension
File & PathMatch common image file extensions: jpg, jpeg, png, gif, webp, svg, avif, bmp, ico, tif, tiff.