gorilla/mux · error
mux: error compiling regex for %q: %w
Error message
mux: error compiling regex for %q: %w
What it means
Returned by newRouteRegexp (regexp.go:109-111) wrapping the underlying error when the per-variable pattern in {name:pattern} fails to compile via RegexpCompileFunc (regexp.Compile by default). The %w lets callers errors.Unwrap to the regexp error. Stored on r.err during route construction.
Source
Thrown at regexp.go:111
}
// Name or pattern can't be empty.
if name == "" || patt == "" {
return nil, fmt.Errorf("mux: missing name or pattern in %q", tag)
}
// Build the regexp pattern.
groupName := varGroupName(groupIdx)
pattern.WriteString(regexp.QuoteMeta(raw) + "(?P<" + groupName + ">" + patt + ")")
// Build the reverse template.
reverse.WriteString(raw + "%s")
// Append variable name and compiled pattern.
varsN[groupIdx] = name
varsR[groupIdx], err = RegexpCompileFunc("^" + patt + "$")
if err != nil {
return nil, fmt.Errorf("mux: error compiling regex for %q: %w", tag, err)
}
}
// Add the remaining.
raw := tpl[end:]
pattern.WriteString(regexp.QuoteMeta(raw))
if options.strictSlash {
pattern.WriteString("[/]?")
}
if typ == regexpTypeQuery {
// Add the default pattern if the query value is empty
if queryVal := strings.SplitN(template, "=", 2)[1]; queryVal == "" {
pattern.WriteString(defaultPattern)
}
}
if typ != regexpTypePrefix {
pattern.WriteByte('$')
}
View on GitHub (pinned to db9d1d0073)
Solutions
- Fix the pattern syntax (balance brackets; use (?:...) for groups).
- Test-compile patterns offline with regexp.Compile before registering.
- If you override RegexpCompileFunc, set it once at init before any route is built (not safe for concurrent use).
Example fix
// before
r.Path("/u/{id:[0-9}")
// -> mux: error compiling regex for "{id:[0-9}": ...
// after
r.Path("/u/{id:[0-9]+}") Defensive patterns
Strategy: try-catch
Validate before calling
func compileCheck(p string) error {
_, err := regexp.Compile(p)
return err
} Type guard
func validPattern(p string) bool { _, err := regexp.Compile(p); return err == nil } Try / catch
rt := r.Path(tpl)
if err := rt.GetError(); err != nil {
return fmt.Errorf("route %q invalid: %w", tpl, err)
} Prevention
- Test-compile every variable pattern at startup.
- Only set RegexpCompileFunc at init, never concurrently.
- Prefer simple character classes over complex RE2.
When it happens
Trigger: r.Path("/u/{id:[0-9}") (unbalanced bracket); r.Path("/u/{s:a(b}"); r.Host("{x:??}"). Any invalid RE2 syntax in a variable pattern.
Common situations: Typing a pattern with unbalanced brackets/parens; using PCRE features RE2 rejects (backreferences, lookahead); overriding RegexpCompileFunc with a stricter engine after routes are built.
Related errors
- mux: duplicated route variable %q
- mux: missing name or pattern in %q
- route %s contains capture groups in its regexp. Only non-cap
- mux: number of parameters must be multiple of 2, got %v
- mux: route already has name %q, can't set %q
AI-assisted analysis of gorilla/mux@db9d1d0073 (2026-08-04).
Data as JSON: /data/errors/53d1523a978b0868.json.
Report an issue: GitHub.