Skip to content

Instantly share code, notes, and snippets.

@splace
Last active February 8, 2019 00:08
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 splace/8ce4b00f75c658aafdb6eebaf5d4bd0f to your computer and use it in GitHub Desktop.
Save splace/8ce4b00f75c658aafdb6eebaf5d4bd0f to your computer and use it in GitHub Desktop.
convert wildcard positional to equivalent regexp string match
// see working example:- https://play.golang.org/p/W5e7rMv6nXv
func WildcardRegExp(w string) string {
return "^" + strings.NewReplacer(
".", "\\.", // escape regexp special chars...
"^", "\\^",
"$", "\\$",
"+", "\\+",
"(", "\\(",
")", "\\)",
"[", "\\[",
"]", "\\]",
"{", "\\{",
"}", "\\}",
"\\\\*", "\\\\.*", // convert wildcard escapes...
"\\\\?", "\\\\.",
"\\*", "\\*",
"\\?", "\\?",
"*", ".*", // convert wildcard specials to regexp equivalent...
"?", ".",
).Replace(w) + "$"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment