{"record":{"id":"3fbce5eaa89fbf05","repo":"caddyserver/caddy","slug":"compiling-matcher-regexp-s-v","errorCode":null,"errorMessage":"compiling matcher regexp %s: %v","messagePattern":"compiling matcher regexp (.+?): (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/caddyhttp/matchers.go","lineNumber":1592,"sourceCode":"\n\t// The regular expression to evaluate, in RE2 syntax,\n\t// which is the same general syntax used by Go, Perl,\n\t// and Python. For details, see\n\t// [Go's regexp package](https://golang.org/pkg/regexp/).\n\t// Captures are accessible via placeholders. Unnamed\n\t// capture groups are exposed as their numeric, 1-based\n\t// index, while named capture groups are available by\n\t// the capture group name.\n\tPattern string `json:\"pattern\"`\n\n\tcompiled *regexp.Regexp\n}\n\n// Provision compiles the regular expression.\nfunc (mre *MatchRegexp) Provision(caddy.Context) error {\n\tre, err := regexp.Compile(mre.Pattern)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"compiling matcher regexp %s: %v\", mre.Pattern, err)\n\t}\n\tmre.compiled = re\n\treturn nil\n}\n\n// Validate ensures mre is set up correctly.\nfunc (mre *MatchRegexp) Validate() error {\n\tif mre.Name != \"\" && !wordRE.MatchString(mre.Name) {\n\t\treturn fmt.Errorf(\"invalid regexp name (must contain only word characters): %s\", mre.Name)\n\t}\n\treturn nil\n}\n\n// Match returns true if input matches the compiled regular\n// expression in mre. It sets values on the replacer repl\n// associated with capture groups, using the given scope\n// (namespace).\nfunc (mre *MatchRegexp) Match(input string, repl *caddy.Replacer) bool {","sourceCodeStart":1574,"sourceCodeEnd":1610,"githubUrl":"https://github.com/caddyserver/caddy/blob/50e54ee279aa1e504fe218ca49ab6ae16c100410/modules/caddyhttp/matchers.go#L1574-L1610","documentation":"MatchRegexp.Provision compiles the `pattern` of path_regexp / header_regexp / query_regexp matchers with regexp.Compile (RE2). An invalid pattern aborts provisioning with the pattern text and Go's compile error. The compiled regex is cached on the matcher; it is also what feeds {http.regexp.<name>.*} placeholders, so it must compile before serving.","triggerScenarios":"@foo path_regexp [unclosed, `a{3,1}`, or PCRE-only syntax like (?<=x) lookbehind and backreferences \\1; header_regexp blocks with patterns mangled by Caddyfile quoting.","commonSituations":"Porting nginx/Perl regex knowledge to Caddy; backslash escaping lost through JSON or shell layers (\\d becoming d); patterns written for the regexp module family assuming PCRE.","solutions":["Validate the pattern with Go's regexp package (RE2) — not with grep -P or an online PCRE tester.","Replace lookarounds/backreferences with RE2-compatible constructs or multiple matchers.","Escape literal { } as \\{ \\} so they are not read as quantifiers.","Check escaping across config layers: JSON needs \\\\d to deliver \\d to the regex compiler."],"exampleFix":"// before (Caddyfile)\n@api path_regexp ^/v(?<ver>\\d+)/users/(?=admin)\n\n// after\n@api path_regexp ^/v(?P<ver>\\d+)/users/admin","handlingStrategy":"validation","validationCode":"import \"regexp\"\n\nfunc validMatcherPattern(p string) bool {\n\t_, err := regexp.Compile(p)\n\treturn err == nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate all path_regexp/header_regexp patterns with Go's regexp.Compile in CI.","Use RE2-compatible syntax; no lookbehind or backreferences.","Escape literal braces; mind backslashes through JSON/shell."],"tags":["caddy","config","regex","re2","matcher"],"backgroundTag":null,"analyzedSha":"50e54ee279aa1e504fe218ca49ab6ae16c100410","analyzedAt":"2026-08-15T09:20:21.641Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}