Last active
February 8, 2019 00:08
-
-
Save splace/8ce4b00f75c658aafdb6eebaf5d4bd0f to your computer and use it in GitHub Desktop.
convert wildcard positional to equivalent regexp string match
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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