{"id":"53d1523a978b0868","repo":"gorilla/mux","slug":"mux-error-compiling-regex-for-q-w","errorCode":null,"errorMessage":"mux: error compiling regex for %q: %w","messagePattern":"mux: error compiling regex for %q: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"regexp.go","lineNumber":111,"sourceCode":"\t\t}\n\n\t\t// Name or pattern can't be empty.\n\t\tif name == \"\" || patt == \"\" {\n\t\t\treturn nil, fmt.Errorf(\"mux: missing name or pattern in %q\", tag)\n\t\t}\n\t\t// Build the regexp pattern.\n\t\tgroupName := varGroupName(groupIdx)\n\n\t\tpattern.WriteString(regexp.QuoteMeta(raw) + \"(?P<\" + groupName + \">\" + patt + \")\")\n\n\t\t// Build the reverse template.\n\t\treverse.WriteString(raw + \"%s\")\n\n\t\t// Append variable name and compiled pattern.\n\t\tvarsN[groupIdx] = name\n\t\tvarsR[groupIdx], err = RegexpCompileFunc(\"^\" + patt + \"$\")\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"mux: error compiling regex for %q: %w\", tag, err)\n\t\t}\n\t}\n\t// Add the remaining.\n\traw := tpl[end:]\n\tpattern.WriteString(regexp.QuoteMeta(raw))\n\tif options.strictSlash {\n\t\tpattern.WriteString(\"[/]?\")\n\t}\n\tif typ == regexpTypeQuery {\n\t\t// Add the default pattern if the query value is empty\n\t\tif queryVal := strings.SplitN(template, \"=\", 2)[1]; queryVal == \"\" {\n\t\t\tpattern.WriteString(defaultPattern)\n\t\t}\n\t}\n\tif typ != regexpTypePrefix {\n\t\tpattern.WriteByte('$')\n\t}\n","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/gorilla/mux/blob/db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265/regexp.go#L93-L129","documentation":"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.","triggerScenarios":"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.","commonSituations":"Typing a pattern with unbalanced brackets/parens; using PCRE features RE2 rejects (backreferences, lookahead); overriding RegexpCompileFunc with a stricter engine after routes are built.","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)."],"exampleFix":"// before\nr.Path(\"/u/{id:[0-9}\")\n// -> mux: error compiling regex for \"{id:[0-9}\": ...\n\n// after\nr.Path(\"/u/{id:[0-9]+}\")","handlingStrategy":"try-catch","validationCode":"func compileCheck(p string) error {\n    _, err := regexp.Compile(p)\n    return err\n}","typeGuard":"func validPattern(p string) bool { _, err := regexp.Compile(p); return err == nil }","tryCatchPattern":"rt := r.Path(tpl)\nif err := rt.GetError(); err != nil {\n    return fmt.Errorf(\"route %q invalid: %w\", tpl, err)\n}","preventionTips":["Test-compile every variable pattern at startup.","Only set RegexpCompileFunc at init, never concurrently.","Prefer simple character classes over complex RE2."],"tags":["routing","config","regexp","build-time"],"analyzedSha":"db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265","analyzedAt":"2026-08-04T21:35:47.097Z","schemaVersion":2}