gohugoio/hugo · error

failed to match regex pattern against string

Error message

failed to match regex pattern against string: %w

What it means

Thrown by strings.CountWords when regexp.MatchString fails against the static CJK detection pattern '\p{Han}|\p{Hangul}|\p{Hiragana}|\p{Katakana}'. Because the pattern is a compile-time constant valid regex, this error is effectively unreachable under normal Go regexp behavior; it would only fire on a regexp engine failure.

Solutions

  1. Re-run the build; if it persists, this indicates a Go runtime/regexp issue, not user input.
  2. Check that the Hugo binary is built against a standard Go toolchain.
  3. Report as a bug if reproducible since the pattern is hardcoded and valid.
  4. Simplify the input string to isolate whether specific bytes trigger the engine failure.
Defensive patterns

Strategy: retry

Prevention

When it happens

Trigger: Theoretically triggered if the Go regexp engine returns an error matching the static CJK pattern against the input string. In practice this should never occur with a stock Hugo build.

Common situations: Essentially never in production. Could appear with a corrupted Go runtime or a non-standard regexp fork. If seen, suspect a runtime/build anomaly rather than user data.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/dfef5ed84cfda522. Report an issue: GitHub.

Appendix: source

Thrown at tpl/strings/strings.go:88

// RuneCount returns the number of runes in s.
func (ns *Namespace) RuneCount(s any) (int, error) {
	ss, err := cast.ToStringE(s)
	if err != nil {
		return 0, fmt.Errorf("failed to convert content to string: %w", err)
	}
	return utf8.RuneCountInString(ss), nil
}

// CountWords returns the approximate word count in s.
func (ns *Namespace) CountWords(s any) (int, error) {
	ss, err := cast.ToStringE(s)
	if err != nil {
		return 0, fmt.Errorf("failed to convert content to string: %w", err)
	}

	isCJKLanguage, err := regexp.MatchString(`\p{Han}|\p{Hangul}|\p{Hiragana}|\p{Katakana}`, ss)
	if err != nil {
		return 0, fmt.Errorf("failed to match regex pattern against string: %w", err)
	}

	if !isCJKLanguage {
		return len(strings.Fields(tpl.StripHTML(ss))), nil
	}

	counter := 0
	for word := range strings.FieldsSeq(tpl.StripHTML(ss)) {
		runeCount := utf8.RuneCountInString(word)
		if len(word) == runeCount {
			counter++
		} else {
			counter += runeCount
		}
	}

	return counter, nil
}

View on GitHub (pinned to 52c9bd7908)