Documentation
¶
Overview ¶
Package wild provides a simple wildcard pattern matching implementation. It supports basic syntax ('?', '*') and extended mode with case-insensitive or non-greedy matching. The package also provides capture groups for wildcards.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaseInsensitive ¶
func CaseInsensitive(m *Matcher)
CaseInsensitive configures the Matcher to perform case-insensitive matching. When this option is applied, the Matcher will treat characters in the pattern and target string as case-insensitive (e.g., 'A' == 'a').
func Extended ¶
func Extended(m *Matcher)
Extended enables extended matching mode. In extended mode, the Matcher supports additional features such as:
- '.' to match exactly one character
- '+' to match one or more characters
- append '?' to a pattern to make it non-greedy
- '\' to escape special characters that follows
Without using Extended, the matcher is in "simple" mode which only supports:
- '*' to match zero or more characters
- '?' to match zero or one character
func Match ¶
Match compiles the given pattern and checks if the subject matches it. Parameters:
- pattern: The pattern string to be compiled into a Matcher.
- subject: The target string to be matched against the compiled pattern.
- opts: Optional configuration options for customizing the behavior of the Matcher.
Returns:
- A boolean indicating whether the subject matches the pattern.
- An error if the pattern compilation fails.
Types ¶
type Matcher ¶
type Matcher struct {
// contains filtered or unexported fields
}
Matcher represents a compiled wildcard pattern with matching rules and state. It contains compiled matching rules, case sensitivity flag, extended mode flag, capture group positions, and the current subject string being matched.
func Compile ¶
Compile compiles a wildcard pattern into a matcher object. Supports following syntax:
- '.' : match exactly one character (extended mode only)
- '?' : match zero or one character (greedy)
- '*' : match zero or more characters (greedy)
- '+' : match one or more characters (greedy, extended mode only)
- '*?' : match zero or more characters (non-greedy, extended mode only)
- '+?' : match one or more characters (non-greedy, extended mode only)
- '\\' : escape special characters (extended mode only)
Options:
- CaseInsensitive: enable case-insensitive matching
- Extended: enable extended mode to support:
- '.' operator for single character
- '+' quantifier for 1-or-more matches
- '\' escapes for special characters
- non-greedy modifiers ('?')
Validates pattern syntax and returns error for:
- invalid escape sequences (e.g., "\\a" where 'a' isn't special)
- dangling escape at pattern end
- invalid quantifier combinations (e.g., "x?+")
- using extended-mode operators (., +, escapes) without Extended
Capture groups are automatically numbered for each wildcard operator, with group 0 always being the full match.
func MustCompile ¶
MustCompile is a helper function that compiles a pattern into a Matcher. It wraps the Compile function and panics if the compilation fails.
This function is intended for use in situations where a failure to compile the pattern is considered a critical error, such as during application initialization. If you want to handle errors gracefully, use Compile instead.
func (*Matcher) Capture ¶
Capture returns the captured substring for the given group index. Index 0 returns the entire matched string, subsequent indices correspond to wildcard operators in the pattern.
func (*Matcher) CapturePos ¶
CapturePos returns the start and end byte positions of a capture group. Positions are [inclusive, exclusive) indices into the original string. Returns (-1, -1) for invalid indices or unmatched groups.
func (*Matcher) Captures ¶
Captures returns the total number of capture groups available. Always returns at least 1 (group 0 for full match). Count includes:
- Group 0: entire match
- Groups 1-N: each wildcard operator in pattern
Example: Pattern "a*?b+" returns 3 (full match, '*?' and '+')
func (Matcher) Dump ¶
Dump writes detailed debugging information about the compiled pattern to the given writer.
Includes pattern string, case sensitivity, rule types (static/greedy/non-greedy), and quantifiers.
Example output:
pattern: a*?b* (case insensitive)
4 rules defined
rule#0: subj=a (static)
rule#1: verb=*; stop=b (non-greedy)
rule#2: subj=b (static)
rune#3: verb=* (greedy)
func (*Matcher) Match ¶
Match attempts to match the input string against the compiled wildcard pattern. Returns true if the entire string matches the pattern rules, or false on any rule mismatch. Empty matcher matches any string.
Source Files
¶
- capture.go
- fold.go
- helper.go
- matcher.go
- option.go
- rule.go