Documentation
¶
Overview ¶
Package reggie provides utilities on top of regexp that make it easier to deal with named capture groups (submatches in Go parlance).
Example (Basic) ¶
This example shows how the Reggie.SubmatchWithName utility provides easy access to the values associated with the capture group names.
package main
import (
"fmt"
"github.com/jsumners/go-reggie"
)
func main() {
regex := reggie.MustCompile(`(?P<foo>\w{3,}) (?P<bar>\w{3,})`)
input := "two words"
matches := regex.FindStringSubmatch(input)
if matches == nil {
panic("did not get any matches!")
}
foo := regex.SubmatchWithName("foo")
bar := regex.SubmatchWithName("bar")
fmt.Printf("found words: (`%s`, `%s`)", foo, bar)
}
Output: found words: (`two`, `words`)
Example (Mapped) ¶
This example shows how the Reggie.GroupMatches utility provides the named capture groups as a map of github.com/samber/mo.Option values.
package main
import (
"fmt"
"github.com/jsumners/go-reggie"
)
func main() {
regex := reggie.MustCompile(`(?P<foo>\w{3,}) (?P<bar>\w{3,})`)
input := "two words"
matches := regex.GroupMatches(input)
if len(matches) != 2 {
panic("did not get any matches!")
}
foo := matches["foo"].MustGet()
bar := matches["bar"].MustGet()
fmt.Printf("found words: (`%s`, `%s`)", foo, bar)
}
Output: found words: (`two`, `words`)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reggie ¶
Reggie is a wrapper for regexp.Regexp.
func Compile ¶
Compile compiles a regular expression string. See regexp.Compile for more detail.
func MustCompile ¶
MustCompile compiles a regular expression string or panics if not possible. See regexp.MustCompile for more detail.
func (*Reggie) FindStringSubmatch ¶
FindStringSubmatch evaluates the regular expression against the provided search string. Internally, this method updates the Reggie instance with the same set of matches as are returned from the function. This allows the Reggie.SubmatchWithName method to be used.
func (*Reggie) GroupMatches ¶
GroupMatches evaluates the regular expression against the provided search string and returns a map of the resulting matches wherein the key names are the names in the named capture groups of the regular expression.
func (*Reggie) SubmatchWithName ¶
SubmatchWithName provides easy access to matches based upon the names of capture groups. The Reggie.FindStringSubmatch method **must** be invoked prior to using this method.