{"record":{"id":"28e6eda504c92e21","repo":"projectdiscovery/katana","slug":"error-compiling-pattern-s-v","errorCode":null,"errorMessage":"error compiling pattern %s: %v","messagePattern":"error compiling pattern (.+?): (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/engine/headless/crawler/normalizer/text_utils.go","lineNumber":48,"sourceCode":"type TextNormalizer struct {\n\t// patterns is a list of regex patterns for the text normalizer\n\tpatterns []*regexp.Regexp\n}\n\n// NewTextNormalizer returns a new TextNormalizer\n//\n// patterns is a list of regex patterns for the text normalizer\n// DefaultTextPatterns is used if patterns is nil. See DefaultTextPatterns for more info.\nfunc NewTextNormalizer() (*TextNormalizer, error) {\n\tpatterns := slices.Clone(DefaultTextPatterns)\n\tpatterns = append(patterns, dateTimePatterns...)\n\n\tvar compiledPatterns []*regexp.Regexp\n\tfor _, pattern := range patterns {\n\t\tpattern := pattern\n\t\tcompiledPattern, err := regexp.Compile(pattern)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error compiling pattern %s: %v\", pattern, err)\n\t\t}\n\t\tcompiledPatterns = append(compiledPatterns, compiledPattern)\n\t}\n\treturn &TextNormalizer{patterns: compiledPatterns}, nil\n}\n\n// Apply applies the patterns to the text and returns the normalized text\nfunc (n *TextNormalizer) Apply(text string) string {\n\tfor _, pattern := range n.patterns {\n\t\tpattern := pattern\n\t\ttext = pattern.ReplaceAllString(text, \"\")\n\t}\n\treturn text\n}\n","sourceCodeStart":30,"sourceCodeEnd":63,"githubUrl":"https://github.com/projectdiscovery/katana/blob/e3e742739c3746f085943ce918fb4e2b8daf6fe6/pkg/engine/headless/crawler/normalizer/text_utils.go#L30-L63","documentation":"NewTextNormalizer compiles each caller-supplied regex pattern with regexp.Compile and fails fast, wrapping the compile error with the offending pattern. It prevents constructing a TextNormalizer with invalid patterns, since compilation errors would otherwise surface per-use at runtime.","triggerScenarios":"Called by New (and tests) when a pattern in the patterns slice is not valid RE2 syntax — e.g. unbalanced parentheses, invalid escape sequences, or Perl-only constructs that Go's regexp rejects.","commonSituations":"User-supplied normalization patterns from config files that contain PCRE-specific syntax like lookaheads ((?=...)), backreferences (\\1), or possessive quantifiers, none of which Go's RE2 supports; truncated patterns; escaped-character mistakes.","solutions":["Read the wrapped error and offending pattern from the message to locate the syntax problem","Validate patterns with regexp.Compile in a preflight/config-load step and report which entry failed","Rewrite PCRE-only constructs in RE2-compatible form (e.g. replace lookaheads with explicit matching)","Test custom patterns against regexp.Compile or an RE2 linter before deployment","Keep patterns simple; prefer literal string handling for non-regex normalization needs"],"exampleFix":"// before\npatterns := []string{\"(?<=user_id=)\\d+\"} // lookbehind unsupported in RE2\n\n// after\npatterns := []string{\"user_id=\\d+\"} // RE2-compatible","handlingStrategy":"validation","validationCode":"for _, p := range patterns {\n\tif _, err := regexp.Compile(p); err != nil {\n\t\treturn fmt.Errorf(\"invalid normalizer pattern %q: %w\", p, err)\n\t}\n}","typeGuard":null,"tryCatchPattern":"norm, err := normalizer.NewTextNormalizer(patterns)\nif err != nil {\n\treturn nil, fmt.Errorf(\"normalizer init failed: %w\", err)\n}","preventionTips":["Validate user-supplied patterns with regexp.Compile at config load","Avoid PCRE-only syntax (lookaheads/lookbehinds, backreferences) — Go uses RE2","Test custom patterns in unit tests before production","Keep patterns minimal; prefer plain string ops where regex is overkill"],"tags":["regex","validation","initialization","go"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"e3e742739c3746f085943ce918fb4e2b8daf6fe6","analyzedAt":"2026-09-03T14:55:13.248Z","contentChangedAt":"2026-09-03T14:55:13.248Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}