Go (RE2)

Kubernetes Label (key=value) in GO

Validate Kubernetes label `key=value` pairs per the K8s naming spec.

Try it in the GO tester →

Pattern

regexGO
^([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)=([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)?$

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)=([a-zA-Z0-9](?:[\w.\-]{0,61}[a-zA-Z0-9])?)?$`)
	input := `app=myapp`
	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

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=myapp

Matches

  • app=myapp

Input

version=v1.2.3

Matches

  • version=v1.2.3

Input

-bad=value

No match

Same pattern, other engines

← Back to Kubernetes Label (key=value) overview (all engines)