Python (re)

Dockerfile ENV Instruction in PY

Match Dockerfile `ENV KEY=value` (or `ENV KEY value`) instructions, capturing key and value.

Try it in the PY tester →

Pattern

regexPY
^ENV\s+([A-Z_][A-Z0-9_]*)(?:[=\s]+)(.+)$   (flags: gm)

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+.

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 3000

Matches

  • ENV NODE_VERSION=20.10.0
  • ENV PORT 3000

Input

ENV DATABASE_URL=postgres://localhost/db

Matches

  • ENV DATABASE_URL=postgres://localhost/db

Input

FROM node:20

No match

Same pattern, other engines

← Back to Dockerfile ENV Instruction overview (all engines)