Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Go glob

License

NotificationsYou must be signed in to change notification settings

gobwas/glob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDocBuild Status

Go Globbing Library.

Install

    go get github.com/gobwas/glob

Example

package mainimport"github.com/gobwas/glob"funcmain() {varg glob.Glob// create simple globg=glob.MustCompile("*.github.com")g.Match("api.github.com")// true// quote meta characters and then create simple globg=glob.MustCompile(glob.QuoteMeta("*.github.com"))g.Match("*.github.com")// true// create new glob with set of delimiters as ["."]g=glob.MustCompile("api.*.com",'.')g.Match("api.github.com")// trueg.Match("api.gi.hub.com")// false// create new glob with set of delimiters as ["."]// but now with super wildcardg=glob.MustCompile("api.**.com",'.')g.Match("api.github.com")// trueg.Match("api.gi.hub.com")// true// create glob with single symbol wildcardg=glob.MustCompile("?at")g.Match("cat")// trueg.Match("fat")// trueg.Match("at")// false// create glob with single symbol wildcard and delimiters ['f']g=glob.MustCompile("?at",'f')g.Match("cat")// trueg.Match("fat")// falseg.Match("at")// false// create glob with character-list matchersg=glob.MustCompile("[abc]at")g.Match("cat")// trueg.Match("bat")// trueg.Match("fat")// falseg.Match("at")// false// create glob with character-list matchersg=glob.MustCompile("[!abc]at")g.Match("cat")// falseg.Match("bat")// falseg.Match("fat")// trueg.Match("at")// false// create glob with character-range matchersg=glob.MustCompile("[a-c]at")g.Match("cat")// trueg.Match("bat")// trueg.Match("fat")// falseg.Match("at")// false// create glob with character-range matchersg=glob.MustCompile("[!a-c]at")g.Match("cat")// falseg.Match("bat")// falseg.Match("fat")// trueg.Match("at")// false// create glob with pattern-alternatives listg=glob.MustCompile("{cat,bat,[fr]at}")g.Match("cat")// trueg.Match("bat")// trueg.Match("fat")// trueg.Match("rat")// trueg.Match("at")// falseg.Match("zat")// false}

Performance

This library is created for compile-once patterns. This means, that compilation could take time, butstrings matching is done faster, than in case when always parsing template.

If you will not use compiledglob.Glob object, and dog := glob.MustCompile(pattern); g.Match(...) every time, then your code will be much more slower.

Rungo test -bench=. from source root to see the benchmarks:

PatternFixtureMatchSpeed (ns/op)
[a-z][!a-x]*cat*[h][!b]*eyes*my cat has very bright eyestrue432
[a-z][!a-x]*cat*[h][!b]*eyes*my dog has very bright eyesfalse199
https://*.google.*https://account.google.comtrue96
https://*.google.*https://google.comfalse66
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}http://yahoo.comtrue163
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}http://google.comfalse197
{https://*gobwas.com,http://exclude.gobwas.com}https://safe.gobwas.comtrue22
{https://*gobwas.com,http://exclude.gobwas.com}http://safe.gobwas.comfalse24
abc*abcdeftrue8.15
abc*affalse5.68
*defabcdeftrue8.84
*defaffalse5.74
ab*efabcdeftrue15.2
ab*efaffalse10.4

The same things withregexp package:

PatternFixtureMatchSpeed (ns/op)
^[a-z][^a-x].*cat.*[h][^b].*eyes.*$my cat has very bright eyestrue2553
^[a-z][^a-x].*cat.*[h][^b].*eyes.*$my dog has very bright eyesfalse1383
^https:\/\/.*\.google\..*$https://account.google.comtrue1205
^https:\/\/.*\.google\..*$https://google.comfalse767
^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$http://yahoo.comtrue1435
^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$http://google.comfalse1674
^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$https://safe.gobwas.comtrue1039
^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$http://safe.gobwas.comfalse272
^abc.*$abcdeftrue237
^abc.*$affalse100
^.*def$abcdeftrue464
^.*def$affalse265
^ab.*ef$abcdeftrue375
^ab.*ef$affalse145

Syntax

Syntax is inspired bystandard wildcards,except that** is aka super-asterisk, that do not sensitive for separators.


[8]ページ先頭

©2009-2025 Movatter.jp