2015-11-30 18:24:20 +03:00
# glob.[go](https://golang.org)
2015-11-30 18:41:06 +03:00
[![GoDoc][godoc-image]][godoc-url] [![Build Status][travis-image]][travis-url]
2015-11-30 18:35:07 +03:00
2016-01-18 13:38:19 +03:00
> Go Globbing Library.
2015-11-30 18:24:20 +03:00
## Install
```shell
go get github.com/gobwas/glob
```
## Example
```go
package main
import "github.com/gobwas/glob"
func main() {
var g glob.Glob
// create simple glob
2016-01-18 13:38:19 +03:00
g = glob.MustCompile("*.github.com")
2015-11-30 18:24:20 +03:00
g.Match("api.github.com") // true
2016-02-25 00:42:32 +03:00
// quote meta characters and then create simple glob
g = glob.MustCompile(glob.QuoteMeta("*.github.com"))
g.Match("*.github.com") // true
2015-11-30 18:24:20 +03:00
// create new glob with set of delimiters as ["."]
2016-02-24 20:23:24 +03:00
g = glob.MustCompile("api.*.com", '.')
2015-11-30 18:24:20 +03:00
g.Match("api.github.com") // true
g.Match("api.gi.hub.com") // false
// create new glob with set of delimiters as ["."]
// but now with super wildcard
2016-02-24 20:23:24 +03:00
g = glob.MustCompile("api.**.com", '.')
2015-11-30 18:24:20 +03:00
g.Match("api.github.com") // true
g.Match("api.gi.hub.com") // true
// create glob with single symbol wildcard
2016-01-18 13:38:19 +03:00
g = glob.MustCompile("?at")
2015-11-30 18:24:20 +03:00
g.Match("cat") // true
g.Match("fat") // true
g.Match("at") // false
2016-02-24 20:23:24 +03:00
// create glob with single symbol wildcard and delimiters ['f']
g = glob.MustCompile("?at", 'f')
2015-11-30 18:24:20 +03:00
g.Match("cat") // true
g.Match("fat") // false
g.Match("at") // false
2016-01-18 13:38:19 +03:00
// create glob with character-list matchers
g = glob.MustCompile("[abc]at")
g.Match("cat") // true
g.Match("bat") // true
g.Match("fat") // false
g.Match("at") // false
// create glob with character-list matchers
g = glob.MustCompile("[!abc]at")
g.Match("cat") // false
g.Match("bat") // false
g.Match("fat") // true
g.Match("at") // false
// create glob with character-range matchers
g = glob.MustCompile("[a-c]at")
g.Match("cat") // true
g.Match("bat") // true
g.Match("fat") // false
g.Match("at") // false
// create glob with character-range matchers
g = glob.MustCompile("[!a-c]at")
g.Match("cat") // false
g.Match("bat") // false
g.Match("fat") // true
g.Match("at") // false
// create glob with pattern-alternatives list
g = glob.MustCompile("{cat,bat,[fr]at}")
g.Match("cat") // true
g.Match("bat") // true
g.Match("fat") // true
g.Match("rat") // true
g.Match("at") // false
g.Match("zat") // false
2015-11-30 18:24:20 +03:00
}
2015-11-30 18:30:08 +03:00
```
## Performance
2016-01-18 13:38:19 +03:00
This library is created for compile-once patterns. This means, that compilation could take time, but
strings matching is done faster, than in case when always parsing template.
2015-11-30 18:30:08 +03:00
2016-01-18 13:38:19 +03:00
If you will not use compiled `glob.Glob` object, and do `g := glob.MustCompile(pattern); g.Match(...)` every time, then your code will be much more slower.
2015-11-30 18:41:06 +03:00
2016-01-18 13:38:19 +03:00
Run `go test -bench=.` from source root to see the benchmarks:
2015-12-01 17:22:17 +03:00
2016-02-26 00:41:17 +03:00
Pattern | Fixture | Match | Speed (ns/op)
--------|---------|-------|--------------
2016-02-26 00:40:45 +03:00
`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my cat has very bright eyes` | `true` | 432
`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my dog has very bright eyes` | `false` | 199
`https://*.google.*` | `https://account.google.com` | `true` | 96
`https://*.google.*` | `https://google.com` | `false` | 66
`{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://yahoo.com` | `true` | 163
`{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://google.com` | `false` | 197
`{https://*gobwas.com,http://exclude.gobwas.com}` | `https://safe.gobwas.com` | `true` | 22
`{https://*gobwas.com,http://exclude.gobwas.com}` | `http://safe.gobwas.com` | `false` | 24
`abc*` | `abcdef` | `true` | 8.15
`abc*` | `af` | `false` | 5.68
`*def` | `abcdef` | `true` | 8.84
`*def` | `af` | `false` | 5.74
`ab*ef` | `abcdef` | `true` | 15.2
`ab*ef` | `af` | `false` | 10.4
2015-11-30 18:41:06 +03:00
2016-01-19 20:52:32 +03:00
The same things with `regexp` package:
2016-02-26 00:41:17 +03:00
Pattern | Fixture | Match | Speed (ns/op)
--------|---------|-------|--------------
2016-02-26 00:40:45 +03:00
`^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` | `my cat has very bright eyes` | `true` | 2553
`^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` | `my dog has very bright eyes` | `false` | 1383
`^https:\/\/.*\.google\..*$` | `https://account.google.com` | `true` | 1205
`^https:\/\/.*\.google\..*$` | `https://google.com` | `false` | 767
2018-09-12 00:33:45 +03:00
`^(https:\/\/.*\.google\..*\|.*yandex\..*\|.*yahoo\..*\|.*mail\.ru)$` | `http://yahoo.com` | `true` | 1435
`^(https:\/\/.*\.google\..*\|.*yandex\..*\|.*yahoo\..*\|.*mail\.ru)$` | `http://google.com` | `false` | 1674
`^(https:\/\/.*gobwas\.com\|http://exclude.gobwas.com)$` | `https://safe.gobwas.com` | `true` | 1039
`^(https:\/\/.*gobwas\.com\|http://exclude.gobwas.com)$` | `http://safe.gobwas.com` | `false` | 272
2016-02-26 00:40:45 +03:00
`^abc.*$` | `abcdef` | `true` | 237
`^abc.*$` | `af` | `false` | 100
`^.*def$` | `abcdef` | `true` | 464
`^.*def$` | `af` | `false` | 265
`^ab.*ef$` | `abcdef` | `true` | 375
`^ab.*ef$` | `af` | `false` | 145
2016-01-19 20:52:32 +03:00
2015-11-30 18:41:06 +03:00
[godoc-image]: https://godoc.org/github.com/gobwas/glob?status.svg
[godoc-url]: https://godoc.org/github.com/gobwas/glob
[travis-image]: https://travis-ci.org/gobwas/glob.svg?branch=master
2016-01-20 16:50:34 +03:00
[travis-url]: https://travis-ci.org/gobwas/glob
## Syntax
Syntax is inspired by [standard wildcards ](http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm ),
2018-09-12 00:33:45 +03:00
except that `**` is aka super-asterisk, that do not sensitive for separators.