glob

package module
v0.0.0-...-2ccc24e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2025 License: MIT Imports: 1 Imported by: 4

README

glob

Package glob provides glob wildcard matching for Go, and is a fork of the github.com/gobwas/glob package.

Eventual goal is a complete rewrite of the actual lexer/parser implementation. Current progress is simply a complete package retooling, cleanup, and API changes to match other packages.

Unit Tests Go Reference Releases Discord Discussion

Install

$ go get github.com/kenshaw/glob

Example

The package can be used like the following:

import "github.com/kenshaw/glob"

g, err := glob.Compile(`test*`)
if err != nil {
    /* ... */
}
fmt.Println(g.Match("test_file.txt"))

// Output:
// true

See the package examples for more examples on using glob.

Syntax

Syntax is meant to be compatible with standard wildcards (as used in Bash or other shells). Additionally, glob supports the "super star" (**) pattern, traversing path separators.

Some examples:

Pattern Test Match
[a-z][!a-x]*cat*[h][!b]*eyes* my cat has very bright eyes true
[a-z][!a-x]*cat*[h][!b]*eyes* my dog has very bright eyes false
https://*.google.* https://account.google.com true
https://*.google.* https://google.com false
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru} http://yahoo.com true
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru} http://google.com false
{https://*example.com,http://exclude.example.com} https://safe.example.com true
{https://*example.com,http://exclude.example.com} http://safe.example.com false
abc* abcdef true
abc* af false
*def abcdef true
*def af false
ab*ef abcdef true
ab*ef af false

Documentation

Overview

Package glob provides glob matching.

Example
package main

import (
	"fmt"

	"github.com/kenshaw/glob"
)

func main() {
	// create simple glob
	g1 := glob.Must("*.github.com")
	fmt.Println(g1.Matcher)
	fmt.Println(g1.Match("api.github.com")) // true

	// quote meta characters and then create simple glob
	g2 := glob.Must(glob.Quote("*.github.com"))
	fmt.Println(g2.Matcher)
	fmt.Println(g2.Match("*.github.com")) // true

	// create new glob with set of delimiters as ["."]
	g3 := glob.Must("api.*.com", '.')
	fmt.Println(g3.Matcher)
	fmt.Println(g3.Match("api.github.com")) // true
	fmt.Println(g3.Match("api.gi.hub.com")) // false

	// create new glob with set of delimiters as ["."]
	// but now with super wildcard
	g4 := glob.Must("api.**.com", '.')
	fmt.Println(g4.Matcher)
	fmt.Println(g4.Match("api.github.com")) // true
	fmt.Println(g4.Match("api.gi.hub.com")) // true

	// create glob with single symbol wildcard
	g5 := glob.Must("?at")
	fmt.Println(g5.Matcher)
	fmt.Println(g5.Match("cat")) // true
	fmt.Println(g5.Match("fat")) // true
	fmt.Println(g5.Match("at"))  // false

	// create glob with single symbol wildcard and delimiters ['f']
	g6 := glob.Must("?at", 'f')
	fmt.Println(g6.Matcher)
	fmt.Println(g6.Match("cat")) // true
	fmt.Println(g6.Match("fat")) // false
	fmt.Println(g6.Match("at"))  // false

	// create glob with character-list matchers
	g7 := glob.Must("[abc]at")
	fmt.Println(g7.Matcher)
	fmt.Println(g7.Match("cat")) // true
	fmt.Println(g7.Match("bat")) // true
	fmt.Println(g7.Match("fat")) // false
	fmt.Println(g7.Match("at"))  // false

	// create glob with character-list matchers
	g8 := glob.Must("[!abc]at")
	fmt.Println(g8.Matcher)
	fmt.Println(g8.Match("cat")) // false
	fmt.Println(g8.Match("bat")) // false
	fmt.Println(g8.Match("fat")) // true
	fmt.Println(g8.Match("at"))  // false

	// create glob with character-range matchers
	g9 := glob.Must("[a-c]at")
	fmt.Println(g9.Matcher)
	fmt.Println(g9.Match("cat")) // true
	fmt.Println(g9.Match("bat")) // true
	fmt.Println(g9.Match("fat")) // false
	fmt.Println(g9.Match("at"))  // false

	// create glob with character-range matchers
	g10 := glob.Must("[!a-c]at")
	fmt.Println(g10.Matcher)
	fmt.Println(g10.Match("cat")) // false
	fmt.Println(g10.Match("bat")) // false
	fmt.Println(g10.Match("fat")) // true
	fmt.Println(g10.Match("at"))  // false

	// create glob with pattern-alternatives list
	g11 := glob.Must("{cat,bat,[fr]at}")
	fmt.Println(g11.Matcher)
	fmt.Println(g11.Match("cat"))  // true
	fmt.Println(g11.Match("bat"))  // true
	fmt.Println(g11.Match("fat"))  // true
	fmt.Println(g11.Match("rat"))  // true
	fmt.Println(g11.Match("at"))   // false
	fmt.Println(g11.Match("zat"))  // false
	fmt.Println(g11.Match("frat")) // false

}
Output:

<suffix:.github.com>
true
<text:`*.github.com`>
true
<btree:[<nothing><-<text:`api.`>-><btree:[<any:![.]><-<text:`.com`>-><nothing>]>]>
true
false
<prefix_suffix:[api.,.com]>
true
true
<row_3:[<single> <text:`at`>]>
true
true
false
<row_3:[<single:![f]> <text:`at`>]>
true
false
false
<row_3:[<list:[abc]> <text:`at`>]>
true
true
false
false
<row_3:[<list:![abc]> <text:`at`>]>
false
false
true
false
<row_3:[<range:[a,c]> <text:`at`>]>
true
true
false
false
<row_3:[<range:![a,c]> <text:`at`>]>
false
false
true
false
<indexed_any_of:[[<text:`cat`> <text:`bat`> <row_3:[<list:[fr]> <text:`at`>]>]]>
true
true
true
true
false
false
false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Quote

func Quote(s string) string

Quote returns a string that quotes all glob pattern meta characters inside the argument text; For example, Quote(`{foo*}`) returns `\[foo\*\]`.

Types

type Glob

type Glob struct {
	syntax.Matcher
	// contains filtered or unexported fields
}

Glob matches glob patterns.

func Compile

func Compile(pattern string, separators ...rune) (*Glob, error)

Compile creates Glob for given pattern and strings (if any present after pattern) as separators. The pattern syntax is:

pattern:
    { term }

term:
    `*`         matches any sequence of non-separator characters
    `**`        matches any sequence of characters
    `?`         matches any single non-separator character
    `[` [ `!` ] { character-range } `]`
                character class (must be non-empty)
    `{` pattern-list `}`
                pattern alternatives
    c           matches character c (c != `*`, `**`, `?`, `\`, `[`, `{`, `}`)
    `\` c       matches character c

character-range:
    c           matches character c (c != `\\`, `-`, `]`)
    `\` c       matches character c
    lo `-` hi   matches character c for lo <= c <= hi

pattern-list:
    pattern { `,` pattern }
                comma-separated (without spaces) patterns

func Must

func Must(pattern string, separators ...rune) *Glob

Must is the same as Compile, except that if Compile returns error, this will panic

func New

func New() *Glob

New creates a new, empty glob.

func (*Glob) MarshalText

func (g *Glob) MarshalText() ([]byte, error)

MarshalText

func (*Glob) String

func (g *Glob) String() string

String satisfies the fmt.Stringer interface.

func (*Glob) UnmarshalText

func (g *Glob) UnmarshalText(buf []byte) error

UnmarshalText satisfies the encoding.TextUnarshaler

Directories

Path Synopsis
cmd
globdraw command
globtest command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL