Help us shape the future of Go! Take the 2026 Go Developer Survey

pattern

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pattern is clover's one matching grammar, shared by every directive that accepts a pattern (include/exclude, verify-branch, checksum, find): a value bracketed by / is a strict RE2 regex; any other value is a glob with *, ?, and <placeholders>. A glob exposes two operations from the same source - Matches (a whole-string filter via gobwas/glob) and capture (the find/replace path, where each <token> becomes a group reachable through Regexp/Tokens). Pure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(template string, values TokenMap) string

Expand fills a replace template, substituting each provided token for its value and leaving the rest untouched.

func HasToken

func HasToken(s string) bool

HasToken reports whether s still contains a <token>, so a caller can reject a replace that referenced an unsupported or unavailable one.

Types

type Kind

type Kind int

Kind distinguishes the two pattern dialects.

const (
	KindGlob  Kind = iota // whole-string glob; a metacharacter-free glob is its own literal
	KindRegex             // RE2, unanchored (substring); bound with ^ and $
)

type Pattern

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

Pattern is a compiled match expression. The two kinds collapse a would-be "literal" kind: a bare glob with no metacharacters already matches exactly, and a backslash escapes the rest. The raw source is retained so a pattern round-trips back to the text the user wrote.

func Compile

func Compile(raw string) (*Pattern, error)

Compile parses a pattern value. A value bracketed by / is compiled as an unanchored RE2 regex; every other value is a whole-string glob that may carry <placeholders>. Glob uses no separators, so * spans every character (including /) - a tag filter matches opaque tokens, not filesystem paths.

func (*Pattern) Kind

func (p *Pattern) Kind() Kind

Kind reports which dialect the pattern was compiled as.

func (*Pattern) Matches

func (p *Pattern) Matches(s string) bool

Matches reports whether s satisfies the pattern: a whole-string glob match, or an unanchored regex search.

func (*Pattern) Regexp

func (p *Pattern) Regexp() *regexp.Regexp

Regexp returns the unanchored regex used to locate and capture: the /regex/ itself, or the glob's <token>-capturing form.

func (*Pattern) String

func (p *Pattern) String() string

String returns the raw pattern text as written by the user.

func (*Pattern) Tokens

func (p *Pattern) Tokens() Tokens

Tokens returns the capture-group token names in order, or nil for a /regex/.

type Token

type Token string

Token is the name of a <placeholder> the grammar recognizes, e.g. <version>.

const (
	TokenCommit          Token = "commit"
	TokenHex             Token = "hex"
	TokenMajor           Token = "major"
	TokenMajorMinor      Token = "major.minor"
	TokenMajorMinorPatch Token = "major.minor.patch"
	TokenMinor           Token = "minor"
	TokenPatch           Token = "patch"
	TokenSHA256          Token = "sha256"
	TokenVersion         Token = "version"
)

Token names the grammar recognizes - the keys of matchRegex, the names a find captures, and the keys Expand substitutes. They are the single source of truth for the vocabulary, shared by the match and checksum packages so a token name is never spelled as a literal.

func (Token) Placeholder added in v0.4.1

func (t Token) Placeholder() string

Placeholder returns the token as it is spelled inside a pattern or a replace template: <version>. Code that builds a template rather than reading one uses it so the angle-bracket spelling stays with the vocabulary above instead of being re-rolled as a literal at every call site.

type TokenMap

type TokenMap map[Token]string

TokenMap maps a token to a substitution value, the input to Expand.

type Tokens

type Tokens []Token

Tokens is an ordered list of token names, as a glob find captures them.

Jump to

Keyboard shortcuts

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