Kubernetes Label (key=value) in PY
Validate Kubernetes label `key=value` pairs per the K8s naming spec.
Try it in the PY tester →Pattern
regexPY
^([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)=([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)?$Python (re) code
pyPython
import re
pattern = re.compile(r"^([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)=([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)?$")
input_text = "app=myapp"
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
Both key and value follow the same rule: 1–63 chars, must start AND end with alphanumeric, can contain underscores/dots/hyphens in the middle. The value group is optional (empty values are allowed). Anchored with ^ and $ for strict full-string matching.
Examples
Input
app=myappMatches
app=myapp
Input
version=v1.2.3Matches
version=v1.2.3
Input
-bad=valueNo match
—