Golang regexp.QuoteMeta() function examples

package regexp

Golang regexp.QuoteMeta() function usage examples.

Note : Useful in situation where you want to do a strings.Join()

Example 1:

 package main

 import (
  "fmt"
  "regexp"
 )

 func main() {

  // regular expression pattern
  str := regexp.QuoteMeta("[boo]")

  fmt.Println("Meta : ", str)

 }

Example 2 :

 regexp.MustCompile(`\b(?:` + strings.Join([]string{
 `go\s+get\s+`,
 `goinstall\s+`,
 regexp.QuoteMeta("http://godoc.org/"),
 regexp.QuoteMeta("http://gopkgdoc.appspot.com/pkg/"),
 regexp.QuoteMeta("http://go.pkgdoc.org/"),
 regexp.QuoteMeta("http://gowalker.org/"),
 }, "|") + `)([-a-zA-Z0-9~+_./]+)`)

Advertisement