Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
Golang

`
(?im)^([a-z0-9\-.]+\.[a-z0-9\-]+:?[0-9]*)?/?(.+)$
`
gm

Description

Used by Zarf to swap the registry domain/port part an image url without needed the docker libraries. usage in go:

func SwapHost(src string, targetHost string) string {
	var parser = regexp.MustCompile(`(?im)^([a-z0-9\-.]+\.[a-z0-9\-]+:?[0-9]*)?/?(.+)$`)
	var substitution = targetHost + "/$2"
	return parser.ReplaceAllString(src, substitution)
}
Submitted by Jeff McCoy - 3 years ago