kovidgoyal/kitty · error

Failed to compile the regex pattern: %#v with error: %w

Error message

Failed to compile the regex pattern: %#v with error: %w

What it means

Thrown when the kitten hints custom regex (built from --hints-regex or a hint type's pattern via functions_for) fails to compile with regexp2 in RE2 mode. The pattern string is included with %#v so you can see exactly what was generated. Typically caused by invalid regex syntax or RE2-incompatible constructs.

Source

Thrown at kittens/hints/marks.go:659

	}
	if self.Pattern != "" {
		return fmt.Sprintf("No %s found with pattern: %s", none_of, self.Pattern)
	}
	return fmt.Sprintf("No %s found", none_of)
}

func find_marks(text string, opts *Options, cli_args ...string) (sanitized_text string, ans []Mark, index_map map[int]*Mark, err error) {
	sanitized_text, hyperlinks := process_escape_codes(text)
	used_pattern := ""

	run_basic_matching := func() error {
		pattern, post_processors, group_processors, err := functions_for(opts)
		if err != nil {
			return err
		}
		r, err := regexp2.Compile(pattern, regexp2.RE2)
		if err != nil {
			return fmt.Errorf("Failed to compile the regex pattern: %#v with error: %w", pattern, err)
		}
		ans = mark(r, post_processors, group_processors, sanitized_text, opts)
		used_pattern = pattern
		return nil
	}

	if opts.CustomizeProcessing != "" {
		cmd := exec.Command(utils.KittyExe(), append([]string{"+runpy", "from kittens.hints.main import custom_marking; custom_marking()"}, cli_args...)...)
		cmd.Stdin = strings.NewReader(sanitized_text)
		stdout, stderr := bytes.Buffer{}, bytes.Buffer{}
		cmd.Stdout, cmd.Stderr = &stdout, &stderr
		err = cmd.Run()
		if err != nil {
			var e *exec.ExitError
			if errors.As(err, &e) && e.ExitCode() == 2 {
				err = run_basic_matching()
				if err != nil {
					return

View on GitHub (pinned to 6d5d0c4406)

Solutions

  1. Fix the reported pattern (printed with %#v) — correct syntax errors like unbalanced groups or escapes
  2. If you need lookarounds/backreferences, remove the regexp2.RE2 flag or rewrite the pattern without them
  3. Test the pattern separately with an RE2-compatible engine (e.g. Go's regexp package) before passing it to the kitten

Example fix

// before
kitty @ kitten hints --custom 'foo(?=bar)'
// after (RE2-safe)
kitty @ kitten hints --custom 'foobar'
Defensive patterns

Strategy: validation

Validate before calling

import "regexp"
func validRE2(pattern string) bool {
    _, err := regexp.Compile(pattern)
    return err == nil
}

Try / catch

if err := find_marks(...); err != nil && strings.Contains(err.Error(), "Failed to compile the regex pattern") { /* surface pattern to user for editing */ }

Prevention

When it happens

Trigger: Passing an invalid regex via opts (e.g. --custom word character that builds a bad pattern), or a pattern using lookahead/lookbehind/backreferences which RE2 mode in regexp2 rejects.

Common situations: User-supplied --hints regex with a typo (unbalanced paren, trailing backslash), or PCRE-only constructs like (?=...) under regexp2.RE2 flag.

Related errors


AI-assisted analysis of kovidgoyal/kitty@6d5d0c4406 (2026-08-27). Data as JSON: /api/errors/76bdb5ad7a33cac9. Report an issue: GitHub.