golang/go · error

found %d matches for %#q in %s

Error message

found %d matches for %#q in %s

What it means

Thrown by the match function (Grep/Stdout/Stderr commands) when -count=N is specified and the actual number of regexp matches differs from N. The %d shows the actual count found, %#q shows the pattern, and %s shows the target name (file for grep, 'stdout'/'stderr' otherwise). This is an assertion that the pattern matches exactly N times.

Source

Thrown at src/cmd/internal/script/cmds.go:696

	pattern := `(?m)` + args[0]
	re, err := regexp.Compile(pattern)
	if err != nil {
		return err
	}

	if isGrep {
		name = args[1] // for error messages
		data, err := os.ReadFile(s.Path(args[1]))
		if err != nil {
			return err
		}
		text = string(data)
	}

	if n > 0 {
		count := len(re.FindAllString(text, -1))
		if count != n {
			return fmt.Errorf("found %d matches for %#q in %s", count, pattern, name)
		}
		return nil
	}

	if !re.MatchString(text) {
		return fmt.Errorf("no match for %#q in %s", pattern, name)
	}

	if !quiet {
		// Print the lines containing the match.
		loc := re.FindStringIndex(text)
		for loc[0] > 0 && text[loc[0]-1] != '\n' {
			loc[0]--
		}
		for loc[1] < len(text) && text[loc[1]] != '\n' {
			loc[1]++
		}
		lines := strings.TrimSuffix(text[loc[0]:loc[1]], "\n")

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run with -v to see actual output and count matches manually to determine the correct N.
  2. Update -count=N to match the new actual count if the change is intentional.
  3. Tighten or loosen the regex pattern so it matches exactly the intended lines.
  4. Omit -count if you only need to assert at least one match rather than an exact count.

Example fix

// Before: stdout -count=2 'error:'
// After:  stdout -count=3 'error:'  // if a new expected error line was added
Defensive patterns

Strategy: validation

Validate before calling

// Pre-count matches in test setup to determine the correct N:
func countMatches(text, pattern string) (int, error) {
    re, err := regexp.Compile(pattern)
    if err != nil { return 0, err }
    return len(re.FindAllString(text, -1)), nil
}
// Then use the discovered count as -count=N in the script.

Prevention

When it happens

Trigger: re.FindAllString(text, -1) returns a slice whose length != n. The test script expected exactly N matches but found a different count.

Common situations: Test output changed (more or fewer log lines, warnings, errors) after a code change, breaking the exact-count assertion. Also occurs when the regex is too broad or too narrow, or when platform-specific output varies the match count.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/083b4dea25cdf4ea. Report an issue: GitHub.