Skip to content

Instantly share code, notes, and snippets.

@thinktainer
Last active December 13, 2017 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinktainer/9167869d72f21ee9d79fe97b6d11684c to your computer and use it in GitHub Desktop.
Save thinktainer/9167869d72f21ee9d79fe97b6d11684c to your computer and use it in GitHub Desktop.
replace_sql_credential.go
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
rg := regexp.MustCompile(`.*:\/\/(\S+):(\S+)@.*`)
str := "sqlserver://user:password@host.example.com/?database=rootschema"
found := rg.FindStringSubmatchIndex(str)
res := str
for i := 2; i < len(found); i = i + 2 {
toRepl := string([]byte(str)[found[i]:found[i+1]])
fmt.Println(toRepl)
res = strings.Replace(res, toRepl, "xxx", 1)
}
fmt.Println(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment