glob

package module
v0.0.0-...-d5b4777 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: MIT Imports: 7 Imported by: 10

README

Oh my glob! Go capture globbing library

GoDoc

This library allows you to use capture groups in globs by using the extended globbing functions.

This is implemented by compiling the glob patterns to regex, and then doing the matching and capturing with the Regexp2 library.

The parser, lexer, and general structure for this library are derived from the excellent https://github.com/gobwas/glob library.

Install

    go get github.com/pachyderm/ohmyglob

Example


package main

import "github.com/pachyderm/ohmyglob"

func main() {
    var g glob.Glob

// create simple glob
    g = glob.MustCompile("*.github.com")
    g.Match("api.github.com") // true

// quote meta characters and then create simple glob
    g = 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") // true
    g.Match("api.gi.hub.com") // false

// create new glob with set of delimiters as ["."]
    // but now with super wildcard
    g = glob.MustCompile("api.**.com", '.')
    g.Match("api.github.com") // true
    g.Match("api.gi.hub.com") // true

    // create glob with single symbol wildcard
    g = glob.MustCompile("?at")
    g.Match("cat") // true
    g.Match("fat") // true
    g.Match("at") // false

// create glob with single symbol wildcard and delimiters ['f']
    g = glob.MustCompile("?at", 'f')
    g.Match("cat") // true
    g.Match("fat") // false
    g.Match("at") // false

// 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

// create glob with extended glob patterns
    g = glob.MustCompile("@(a)")
    g.Match("") // false
    g.Match("a") // true
    g.Match("aa") // false
    g.Match("aaa") // false
    g.Match("aaX") // false
    g.Match("bbb") // false

    g = glob.MustCompile("*(a)")
    g.Match("") // true
    g.Match("a") // true
    g.Match("aa") // true
    g.Match("aaa") // true
    g.Match("aaX") // false
    g.Match("bbb") // false

    g = glob.MustCompile("+(a)")
    g.Match("") // false
    g.Match("a") // true
    g.Match("aa") // true
    g.Match("aaa") // true
    g.Match("aaX") // false
    g.Match("bbb") // false

    g = glob.MustCompile("?(a)")
    g.Match("") // true
    g.Match("a") // true
    g.Match("aa") // false
    g.Match("aaa") // false
    g.Match("aaX") // false
    g.Match("bbb") // false

    g = glob.MustCompile("!(a)")
    g.Match("") // true
    g.Match("a") // false
    g.Match("aa") // true
    g.Match("aaa") // true
    g.Match("aaX") // true
    g.Match("bbb") // true

// create a glob and get the capture groups
    g = glob.MustCompile("test/a*(a|b)/*(*).go")
    g.Capture("test/aaaa/x.go") // ["test/aaaa/x.go", "aaa", "x"]

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QuoteMeta

func QuoteMeta(s string) string

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

Types

type Glob

type Glob struct {
	// contains filtered or unexported fields
}

Glob represents compiled glob pattern.

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

extended-glob:
    `(` { `|` pattern } `)`
    `@(` { `|` pattern } `)`
                match and capture one of pipe-separated subpatterns
    `*(` { `|` pattern } `)`
                match and capture any number of the pipe-separated subpatterns
    `+(` { `|` pattern } `)`
                match and capture one or more of the pipe-separated subpatterns
    `?(` { `|` pattern } `)`
                match and capture zero or one of the pipe-separated subpatterns
    `!(` { `|` pattern } `)`
                match and capture anything except one of the pipe-separated subpatterns

func MustCompile

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

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

func (*Glob) Capture

func (g *Glob) Capture(fixture string) []string

Capture returns the list of subexpressions captured while testing the fixture against the compiled pattern

func (*Glob) Match

func (g *Glob) Match(fixture string) bool

Match tests the fixture against the compiled pattern, and return true for a match

func (*Glob) Replace

func (g *Glob) Replace(fixture, template string) string

Replace runs the compiled pattern on the given fixture, and then replaces any instance of $n (or ${n}) in the template with the nth capture group

Directories

Path Synopsis
ast

Jump to

Keyboard shortcuts

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