Redglob is a simple glob-style pattern matcher library for Go, inspired by Redis's pattern matching implementation. It provides a fast and easy-to-use solution for matching strings and byte slices against patterns with wildcard support.
- Unicode support
- Case-insensitive matching
- Capability to match strings and byte slices
- Supports
*
,?
, character classes[abc]
, ranges[a-z]
, and negation[^abc]
go get github.com/maolonglong/redglob
package main
import (
"fmt"
"github.com/maolonglong/redglob"
)
func main() {
pattern := "h?ll*"
str := "hello, world!"
if redglob.Match(str, pattern) {
fmt.Println("Match!")
} else {
fmt.Println("No match.")
}
}
Match(str, pattern string) bool
: returns true if the given string matches the pattern.MatchFold(str, pattern string) bool
: case-insensitive version ofMatch
.MatchBytes(b []byte, pattern string) bool
: similar toMatch
, but accepts a byte slice instead of a string.MatchBytesFold(b []byte, pattern string) bool
: case-insensitive version ofMatchBytes
.
Redglob's pattern syntax is similar to that of Redis's KEYS
command:
*
matches any sequence of non-Separator characters?
matches any single non-Separator characterc
matches characterc
(wherec
is any character except*
,?
, and\
)\c
matches characterc
[abc]
matchesa
orb
orc
[^abc]
matches any character excepta
,b
, orc
[a-z]
matchesa
toz
[^a-z]
matches any character excepta
toz
Redglob is implemented in pure Go and is optimized for performance. It uses a simple and efficient algorithm to match patterns against strings, and takes advantage of Go's built-in Unicode support to handle Unicode characters correctly.
Redglob is licensed under the Apache License, Version 2.0. See the LICENSE file for details.