Go hello world with regex
FluxCD Image policy applies regex to select latest tag. FluxCD uses go regex, so this go app tests a regex.
Create project
# create go module
go mod init atoss-regex
# create main file
touch main.go
Code
// main.go
package main
import (
"fmt"
"regexp"
)
func main() {
// pattern := `^(?P<version>[0-9]+\.[0-9]+\.[0-9]+)$`
pattern := `^(?P<version>[0-9]+\\.[0-9]+\\.[0-9]+)$`
// pattern := `^([0-9]+\\.[0-9]+\\.[0-9]+)$`
tags := []string{
"v1.0.0",
"0.0.51",
"0.0.16",
"2.1.0",
"1.2.3",
"v0.9.8",
"1.0",
"invalid",
"1.0.0-beta",
}
re, err := regexp.Compile(pattern)
if err != nil {
fmt.Println("Error compiling regex:", err)
return
}
for _, tag := range tags {
if re.MatchString(tag) {
fmt.Printf("Tag '%s' matches the pattern.\n", tag)
} else {
fmt.Printf("Tag '%s' does not match the pattern.\n", tag)
}
}
}
Build and Run
# run
go run main.go
# build
go build -o app
./app