golang/go · error

no match for %#q in %s

Error message

no match for %#q in %s

What it means

Thrown by the match function (Grep/Stdout/Stderr commands) when the regexp pattern does not match anywhere in the target text and -count=N was not specified. re.MatchString returns false. The %#q shows the pattern and %s shows the target name. This is the basic 'pattern must appear at least once' assertion.

Source

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

	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")
		s.Logf("matched: %s\n", lines)
	}
	return nil
}

// Help writes command documentation to the script log.

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run with -v to see actual stdout/stderr and compare against the expected pattern.
  2. Fix the regex if it's too strict — check anchors, character classes, and escaping.
  3. Verify the output stream: the pattern may be on stderr, not stdout (use stderr instead of stdout).
  4. If the program's output legitimately changed, update the expected pattern to match.

Example fix

// Before: stdout 'BUILD SUCCESSFUL'
// After:  stdout 'build completed'  // if message wording changed

// Or check the right stream:
// stderr 'error:'  // instead of stdout
Defensive patterns

Strategy: validation

Validate before calling

// Pre-test: verify the pattern matches expected output:
func verifyPattern(text, pattern string) error {
    re, err := regexp.Compile(pattern)
    if err != nil { return err }
    if !re.MatchString(text) {
        return fmt.Errorf("pattern %q does not match output", pattern)
    }
    return nil
}

Prevention

When it happens

Trigger: re.MatchString(text) returns false — the compiled regexp finds no match in stdout, stderr, or the grep target file. No -count constraint was set (n == 0), so the default 'at least one match' expectation applies.

Common situations: Program output changed so an expected line no longer appears. Regex pattern is wrong (escaping issues, wrong anchors, case sensitivity). Output went to stderr instead of stdout (or vice versa). Platform-specific output differs from what the test expects.

Related errors


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