{"record":{"id":"8e435f5b07be83ef","repo":"projectdiscovery/nuclei","slug":"could-not-compile-regex-s-8e435f","errorCode":null,"errorMessage":"could not compile regex: %s","messagePattern":"could not compile regex: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/operators/matchers/compile.go","lineNumber":53,"sourceCode":"\t// Validate the matcher structure\n\tif err := matcher.Validate(); err != nil {\n\t\treturn err\n\t}\n\n\t// By default, match on body if user hasn't provided any specific items\n\tif matcher.Part == \"\" && matcher.GetType() != DSLMatcher {\n\t\tmatcher.Part = \"body\"\n\t}\n\n\t// Compile the regexes (with shared cache)\n\tfor _, regex := range matcher.Regex {\n\t\tif cached, err := cache.Regex().GetIFPresent(regex); err == nil && cached != nil {\n\t\t\tmatcher.regexCompiled = append(matcher.regexCompiled, cached)\n\t\t\tcontinue\n\t\t}\n\t\tcompiled, err := regexp.Compile(regex)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"could not compile regex: %s\", regex)\n\t\t}\n\t\t_ = cache.Regex().Set(regex, compiled)\n\t\tmatcher.regexCompiled = append(matcher.regexCompiled, compiled)\n\t}\n\n\t// Compile and validate binary Values in matcher\n\tfor _, value := range matcher.Binary {\n\t\tif decoded, err := hex.DecodeString(value); err != nil {\n\t\t\treturn fmt.Errorf(\"could not hex decode binary: %s\", value)\n\t\t} else {\n\t\t\tmatcher.binaryDecoded = append(matcher.binaryDecoded, string(decoded))\n\t\t}\n\t}\n\n\t// Compile the dsl expressions (with shared cache)\n\tfor _, dslExpression := range matcher.DSL {\n\t\tif cached, err := cache.DSL().GetIFPresent(dslExpression); err == nil && cached != nil {\n\t\t\tmatcher.dslCompiled = append(matcher.dslCompiled, cached)","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/projectdiscovery/nuclei/blob/265b3a3dec374741614e342f813c10f8b38d2bb7/pkg/operators/matchers/compile.go#L35-L71","documentation":"Template compilation error from Matcher.CompileMatchers (pkg/operators/matchers/compile.go:53). Each entry of the matcher's `regex:` list is compiled with the stdlib RE2 regexp.Compile (with a shared cache). Unparseable patterns abort matcher compilation with the offending pattern.","triggerScenarios":"A matcher regex containing PCRE-only constructs (backreferences \\1, lookarounds (?=...) (?<=...)) or syntax errors such as unbalanced parens, a dangling '*', or an invalid escape.","commonSituations":"Copying detection regexes written for PCRE (Snort/ModSecurity/Python rules) into templates; YAML double-quote escaping eating backslashes (\"\\d\" becomes literal 'd' issues); partially rendered dynamic regexes (e.g. payloads with regex metachars).","solutions":["Test each regex with Go's RE2 engine (regex101 'golang' flavor) and fix/remove unsupported constructs","In YAML prefer single-quoted strings so \\d, \\s, \\+ pass through verbatim","When a regex is composed from variables, escape interpolated values (regexp.QuoteMeta equivalent in DSL: `re_quote` if available, or precompute)","Run `nuclei -validate -t template.yaml` to get the exact failing pattern"],"exampleFix":"# before\nregex:\n  - '(?<=v)1\\.\\d+'\n# after\nregex:\n  - 'v(1\\.\\d+)'","handlingStrategy":"validation","validationCode":"for _, r := range m.Regex {\n\tif _, err := regexp.Compile(r); err != nil {\n\t\treturn fmt.Errorf(\"bad matcher regex %q: %w\", r, err)\n\t}\n}","typeGuard":"func matcherRegexCompiles(pattern string) bool { _, err := regexp.Compile(pattern); return err == nil }","tryCatchPattern":"if err := m.CompileMatchers(); err != nil && strings.Contains(err.Error(), \"could not compile regex\") {\n\t// pull pattern from message; flag RE2 incompatibility (backrefs/lookarounds)\n}","preventionTips":["Author regexes against RE2 semantics (golang flavor testers)","Single-quote YAML regex strings to protect escapes","Escape dynamic fragments before splicing them into patterns"],"tags":["matcher","regex","re2","template","compile-time"],"backgroundTag":null,"analyzedSha":"265b3a3dec374741614e342f813c10f8b38d2bb7","analyzedAt":"2026-08-15T20:05:51.855Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}