regexutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package regexutil provides utilities for safe regex handling.

Index

Constants

View Source
const (
	// MaxPatternLength is the maximum allowed regex pattern length.
	MaxPatternLength = 1000

	// MaxRepetitionCount is the maximum allowed repetition count (e.g., a{1000}).
	MaxRepetitionCount = 100

	// MaxNestedQuantifiers is the maximum depth of nested quantifiers.
	MaxNestedQuantifiers = 3

	// CompileTimeout is the maximum time to spend compiling a regex.
	CompileTimeout = 100 * time.Millisecond
)

Limits for regex patterns to prevent ReDoS attacks.

Variables

This section is empty.

Functions

func MustSafeCompile

func MustSafeCompile(pattern string) *regexp.Regexp

MustSafeCompile is like SafeCompile but panics on error. Only use for patterns known at compile time.

func SafeCompile

func SafeCompile(pattern string) (*regexp.Regexp, error)

SafeCompile validates and compiles a regex pattern with safety checks. Returns the compiled regex or an error if the pattern is unsafe or invalid.

func SafeCompileWithFlags

func SafeCompileWithFlags(pattern string, flags string) (*regexp.Regexp, error)

SafeCompileWithFlags validates and compiles a regex with the given flags prefix. Common flags: "(?i)" for case-insensitive, "(?m)" for multiline.

func ValidatePattern

func ValidatePattern(pattern string) error

ValidatePattern checks if a regex pattern is safe to compile and execute. Returns nil if the pattern is safe, or a ValidationError explaining the issue.

Types

type ValidationError

type ValidationError struct {
	Pattern string
	Reason  string
}

ValidationError represents a regex validation error.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

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