Skip to content

Reference

Exact, lookup-oriented facts about regexutil: every exported symbol, every bound, every error, and what each one does when the input is wrong.

Godoc — signatures, parameter names, and the package doc comment — lives on pkg.go.dev and is not duplicated here. What is here is the behaviour godoc does not state: the effective bounds when several deadlines are in play, the exact error strings, and the edge cases.

The complete exported surface

regexutil exports seven symbols and nothing else. There is no configuration struct, no options pattern, no package-level variable you can set.

Symbol Kind Value / signature
MaxPatternLength const (untyped int) 1024
DefaultCompileTimeout const (time.Duration) 100 * time.Millisecond
ErrPatternTooLong var error "regex pattern exceeds maximum length"
ErrPatternCompileTimeout var error "regex pattern compile timed out"
ErrPatternInvalid var error "regex pattern is invalid"
CompileBounded func (ctx context.Context, pattern string) (*regexp.Regexp, error)
CompileBoundedTimeout func (pattern string, timeout time.Duration) (*regexp.Regexp, error)

Both constants are Go const declarations. They are compile-time values of the regexutil package, so a consumer can read them but cannot change them — see Can I change the maximum pattern length?.

What the two functions return

Both functions return the same pair, with the same guarantees:

  • On success — a non-nil *regexp.Regexp and a nil error. The value is an ordinary regexp.Regexp produced by regexp.Compile; regexutil does not wrap it, restrict it, or attach anything to it. Every method on it behaves exactly as it would had you called regexp.Compile yourself.
  • On failure — a nil *regexp.Regexp and a non-nil error wrapping one of the three sentinels. There is no partial-success case: you never get a usable regex alongside an error.

CompileBoundedTimeout delegates to CompileBounded after building a context, so every rule on this page that applies to one applies to the other.

Is regexutil safe for concurrent use?

Yes. Neither function touches shared state — each call creates its own context, channel and goroutine, and the constants are immutable. Any number of goroutines may call either function at the same time.

Note that this says nothing about load: regexutil bounds each individual compile, not the number of compiles in flight. See What regexutil does not do.

In this section

  • Limits and timeouts — the two bounds, what actually applies when a caller deadline is also in play, and what happens at every edge (zero timeout, cancelled context, nil context, empty pattern).
  • Errors — the three sentinels, their exact message text, the hints attached to them, what errors.Is does and does not match, and which error kind echoes the pattern back at you.