Documentation
¶
Overview ¶
Package regex provides a wrapper around Go's native regular expression package, offering a more structured interface for compiling, matching, validating, and replacing text using regular expressions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrParse error = &ParseError{} ErrSyntax error = &SyntaxError{} )
Functions ¶
This section is empty.
Types ¶
type ParseError ¶
type ParseError struct {
Input string // The input string that failed to match
Regex string // The regular expression pattern
}
ParseError indicates that the input string does not match the regular expression.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error marks ParseError as an error type and returns the error message.
func (*ParseError) Is ¶
func (e *ParseError) Is(err error) bool
Is overrides the behavior of `errors.Is` in order to the error to comparable with ErrParse.
type Regex ¶
type Regex interface {
fmt.Stringer
// Regexp returns the Go native regular expression object
Regexp() *regexp.Regexp
// String returns the source text used to compile the regular expression.
String() string
// Match matches the regular expression against given string. If a match could be
// found, a [Result] object is returned, otherwise nil.
Match(s string) Result
// TryMatch matches the regular expression against given string. If returns
// a [Result] object or an error if no match could be found.
TryMatch(s string) (Result, error)
// Test checks if the given string matches the regular expression.
Test(s string) bool
// Validate checks if the given string matches the regular expression.
// If not, an error is returned.
Validate(s string) error
// ReplaceAll returns a copy of s, replacing matches of the
// regular expression with the replacement string repl.
// The replacement string can contain $-substitutions.
ReplaceAll(s string, repl string) string
// ReplaceAllLiteral returns a copy of s, replacing matches of the
// regular expression with the replacement string repl.
// The replacement string is substituted literally, with no
// special processing for $ signs.
ReplaceAllLiteral(src string, repl string) string
// contains filtered or unexported methods
}
func Compile ¶
Compile parses a regular expression and returns, if successful, a Regex object that can be used to match against text.
func MustCompile ¶
MustCompile parses a regular expression and returns, if successful, a Regex object that can be used to match against text.
type Result ¶
type Result interface {
// Matches returns a list of all sub matches.
Matches() []string
// Groups returns a map containing the extracted value for each named capture group.
Groups() map[string]string
// Group returns the extracted value for the named capture group.
Group(name string) (string, bool)
// GroupOr returns the extracted value for the named capture group or the fallback value.
GroupOr(name string, fallback string) string
// GroupOrEmpty returns the extracted value for the named capture group or an empty string.
GroupOrEmpty(name string) string
// GroupInt returns the extracted value for the named capture group as an integer.
GroupInt(name string) (int, bool)
// GroupIntOr returns the extracted value for the named capture group as an integer or the fallback value.
GroupIntOr(name string, fallback int) int
// GroupIntOrZero returns the extracted value for the named capture group as an integer or zero.
GroupIntOrZero(name string) int
// GroupFloat returns the extracted value for the named capture group as a float.
GroupFloat(name string) (float64, bool)
// GroupFloatOr returns the extracted value for the named capture group as a float or the fallback value.
GroupFloatOr(name string, fallback float64) float64
// GroupFloatOrZero returns the extracted value for the named capture group as a float or zero.
GroupFloatOrZero(name string) float64
}
type SyntaxError ¶
type SyntaxError struct {
Err error
}
SyntaxError indicates that there was a syntax error in the regular expression.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
Error marks SyntaxError as an error type and returns the error message.
func (*SyntaxError) Is ¶
func (e *SyntaxError) Is(err error) bool
Is overrides the behavior of `errors.Is` in order to the error to comparable with ErrSyntax.