Skip to content

Instantly share code, notes, and snippets.

@flowck
Created September 2, 2022 10:31
Show Gist options
  • Save flowck/9a76d3640950bd52cf41ef3d31a8c6d0 to your computer and use it in GitHub Desktop.
Save flowck/9a76d3640950bd52cf41ef3d31a8c6d0 to your computer and use it in GitHub Desktop.
Regex to match a slug in Go (Lang)
package main
import (
"fmt"
"log"
"regexp"
)
// Regex source: https://ihateregex.io/expr/url-slug/
func main() {
str := "Hello my new % calendar id 09"
str2 := "hello-this-is-my-slug"
isMatched, err := regexp.Match("^[a-z/d]+(?:-[a-z/d]+)*$", []byte(str2))
if err != nil {
log.Fatal(err)
}
fmt.Println(str)
fmt.Println(str2)
fmt.Println("Is matched: ", isMatched)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment