regexpset

package module
v0.0.0-...-4d4ec8e Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2020 License: Apache-2.0 Imports: 3 Imported by: 12

README

regexpset LicenseGo.Dev referenceTravisCIGo Report Card

What is it?

regexpset is an extension to Go standard library regexp. It contains a set of regular expression patterns and provides methods for matching on input string.

Basic example

    
    import "github.com/sugarme/regexpset"

    func main(){
        var patterns []string = []string{
            `\w+`,
            `\d+`,
            `\pL+`,
            `foo`,
            `bar`,
            `barfoo`,
            `foobar`,
        }

        set, err := regexpset.NewRegexpSet(patterns)
        if err != nil {
            log.Fatal(err)
        }

        setmatches := set.Matches("foobar")

        fmt.Println(setmatches.Matches())

        // [0 1 2 3 4 6]
    }
   

Documentation

See Go.Dev Reference

License: Apache 2.0

Documentation

Overview

Package regexpset is an extension to Go standard library `regexp`. It contains a set of regular expression patterns and provides methods for matching on input string.

## Basic example

import "github.com/sugarme/regexpset"

func main(){
    var patterns []string = []string{
        `\w+`,
        `\d+`,
        `\pL+`,
        `foo`,
        `bar`,
        `barfoo`,
        `foobar`,
    }

    set, err := regexpset.NewRegexpSet(patterns)
    if err != nil {
        t.Log(err)
    }

    setmatches := set.Matches("foobar")

    fmt.Println(setmatches.Matches())

    // [0 1 2 3 4 6]
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RegexpSet

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

RegexpSet contains a set of regular expression set for matching.

func NewRegexpSet

func NewRegexpSet(patterns []string) (retVal *RegexpSet, err error)

NewRegexpSet creates a new RegexpSet from a slice of patterns

Example
package main

import (
	"log"

	"github.com/sugarme/regexpset"
)

func main() {

	var patterns []string = []string{
		`[a-z]+@[a-z]+\.(com|org|net)`,
		`[a-z]+\.(com|org|net)`,
	}

	_, err := regexpset.NewRegexpSet(patterns)
	if err != nil {
		log.Fatal(err)
	}
}

func (RegexpSet) IsMatch

func (rs RegexpSet) IsMatch(s string) bool

IsMatch returns true if and only if one of the regular expressions in this set matches the given input text.

Example
package main

import (
	"fmt"
	"log"

	"github.com/sugarme/regexpset"
)

func main() {

	var patterns []string = []string{
		`[a-z]+@[a-z]+\.(com|org|net)`,
		`[a-z]+\.(com|org|net)`,
	}

	set, err := regexpset.NewRegexpSet(patterns)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(set.IsMatch("foo@example.com"))

}
Output:
true

func (RegexpSet) IsMatchAt

func (rs RegexpSet) IsMatchAt(s string, start int) bool

IsMatchAt returns the same as `IsMatch`, but starts the search at the given offset.

NOTE. the significance of the starting point is that it takes the surrounding context into consideration. E.g. `\A` anchor can only match when `start == 0`.

Example
package main

import (
	"fmt"
	"log"

	"github.com/sugarme/regexpset"
)

func main() {
	var patterns []string = []string{`\Aexample`}
	set, err := regexpset.NewRegexpSet(patterns)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(set.IsMatchAt("foo@example.com", 4))

}
Output:
true

func (RegexpSet) Len

func (rs RegexpSet) Len() int

Len returns total number of regular expression in this set.

Example
package main

import (
	"fmt"
	"log"

	"github.com/sugarme/regexpset"
)

func main() {

	var patterns []string = []string{
		`[a-z]+@[a-z]+\.(com|org|net)`,
		`[a-z]+\.(com|org|net)`,
	}

	set, err := regexpset.NewRegexpSet(patterns)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(set.Len())

}
Output:
2

func (RegexpSet) Matches

func (rs RegexpSet) Matches(s string) (retVal SetMatches)

Matches returns indexes of patterns those match input string

Example
package main

import (
	"fmt"
	"log"

	"github.com/sugarme/regexpset"
)

func main() {
	var patterns []string = []string{
		`[a-z]+@[a-z]+\.(com|org|net)`,
		`[a-z]+\.(com|org|net)`,
	}

	set, err := regexpset.NewRegexpSet(patterns)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(set.Matches("foo@example.com").Matches())

}
Output:
[0 1]

func (RegexpSet) Patterns

func (rs RegexpSet) Patterns() []string

Patterns returns the patterns this set will match on.

func (RegexpSet) ReadMatchesAt

func (rs RegexpSet) ReadMatchesAt(s string, start int) bool

ReadMatchesAt returns the same as `Matches`, but start the search at a given offset.

NOTE. the significance of the starting point is that it takes the surrounding context into consideration. For example, the `/A` anchor can only match when `start == 0`.

This method returns `true` if and only if at least one member of `matches` is `true` after executing the set against input string.

Example
package main

import (
	"fmt"
	"log"

	"github.com/sugarme/regexpset"
)

func main() {
	var patterns1 []string = []string{`\Aexample`, `zzz`}
	var patterns2 []string = []string{`\Aaaa`, `zzz`}
	set1, err := regexpset.NewRegexpSet(patterns1)
	if err != nil {
		log.Fatal(err)
	}

	set2, err := regexpset.NewRegexpSet(patterns2)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(set1.ReadMatchesAt("foo@example.com", 4))
	fmt.Println(set2.ReadMatchesAt("foo@example.com", 4))

}
Output:
true
false

type SetMatches

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

SetMatches is a set of matches returned by a RegexpSet

func (SetMatches) Iter

func (sm SetMatches) Iter() SetMatchesIter

Iter returns an iterator over the indexes in the regular expression that matched. This will always produce matches in ascending order of index, where the index corresponds to the index of the regular expressions that matched with respect to its position when initial building the set.

func (SetMatches) Len

func (sm SetMatches) Len() int

Len returns the total number of regular expressions in the set that created these matches.

func (SetMatches) Matched

func (sm SetMatches) Matched(patternIdx int) bool

Matched returns whether the regular expression pattern at given index matched The index for a regular expression pattern is determined by its insersion order upon the initial construction of a `RegexpSet`, starting from 0. It will be panic if patternIdx is out of bound.

func (SetMatches) MatchedAny

func (sm SetMatches) MatchedAny() bool

MatchedAny returns whether this set contains any matches.

Example
package main

import (
	"fmt"
	"log"

	"github.com/sugarme/regexpset"
)

func main() {
	var patterns []string = []string{
		`\w+`,
		`\d+`,
		`\pL+`,
		`foo`,
		`bar`,
		`barfoo`,
		`foobar`,
	}

	set, err := regexpset.NewRegexpSet(patterns)
	if err != nil {
		log.Fatal(err)
	}

	sm := set.Matches("foobar")
	fmt.Println(sm.MatchedAny())

}
Output:
true

func (SetMatches) Matches

func (sm SetMatches) Matches() (retVal []int)

Matches returns indexes of regular expression that matched.

Example
package main

import (
	"fmt"
	"log"

	"github.com/sugarme/regexpset"
)

func main() {
	var patterns []string = []string{
		`\w+`,
		`\d+`,
		`\pL+`,
		`foo`,
		`bar`,
		`barfoo`,
		`foobar`,
	}

	set, err := regexpset.NewRegexpSet(patterns)
	if err != nil {
		log.Fatal(err)
	}

	sm := set.Matches("foobar")
	fmt.Println(sm.Matched(4))

}
Output:
true

type SetMatchesIter

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

SetMatchesIter is an SetMatches iterator

func (*SetMatchesIter) Next

func (smi *SetMatchesIter) Next() (retVal int, ok bool)

Next implement iterator for SetMatchesIter It returns INDEX of regular expression pattern that matched. It will return a INDEX value = -1 if not matched.

Jump to

Keyboard shortcuts

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