reggie

package module
v1.0.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 2 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Reggie

A simple wrapper on top of regexp.Regexp to make working with named capture groups easier.

See example_basic_test.go for a full example.

Install

$ go get github.com/jsumners/go-reggie

Example

package main

import "github.com/jsumners/go-reggie"
import "fmt"

func main() {
	regex := reggie.MustCompile(`(?P<foo>\w{3})`)
	matches := regex.FindStringSubmatch("bar")
	
	if matches == nil {
		panic("💥")
	}
		
	foo := regex.SubmatchWithName("foo")
	fmt.Println(foo)
	
	// Output:
	// bar
}

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

type Reggie struct {
	*regexp.Regexp
	// contains filtered or unexported fields
}

Reggie is a wrapper for regexp.Regexp.

func Compile

func Compile(input string) (*Reggie, error)

Compile compiles a regular expression string. See regexp.Compile for more detail.

func MustCompile

func MustCompile(input string) *Reggie

MustCompile compiles a regular expression string or panics if not possible. See regexp.MustCompile for more detail.

func (*Reggie) FindStringSubmatch

func (re *Reggie) FindStringSubmatch(input string) []string

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

func (re *Reggie) GroupMatches(input string) map[string]mo.Option[string]

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

func (re *Reggie) SubmatchWithName(name string) string

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.

Jump to

Keyboard shortcuts

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