Coder Social home page Coder Social logo

ohmyglob's Introduction

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"]

}

ohmyglob's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ohmyglob's Issues

tag?

Hi, I was wondering if you could you add a go.mod file and make a tag?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.