gohugoio/hugo · warning

failed to match regex pattern against string: %w

Error message

failed to match regex pattern against string: %w

What it means

Inside `strings.CountWords`, Hugo runs `regexp.MatchString` with a fixed CJK-detection pattern (`\p{Han}|\p{Hangul}|\p{Hiragana}|\p{Katakana}`) to decide between whitespace-based and rune-based word counting. This error wraps a failure from that match. Since the pattern is a hard-coded valid regex, `regexp.MatchString` compiling/matching it essentially cannot fail in practice — this branch is defensive and nearly unreachable in released Hugo versions.

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 8a468df065)

Solutions

  1. Verify you're running an official Hugo release (`hugo version`); reinstall/redownload the binary if it may be corrupted.
  2. If using a fork or custom build, diff `tpl/strings/strings.go` against upstream — the CJK regex at strings.go:86 has likely been modified into an invalid pattern.
  3. If reproducible on an official build, file a Hugo issue with the wrapped error text and a minimal site.
Defensive patterns

Strategy: validation

Validate before calling

// pre-validate the pattern outside the hot path
{{ $pattern := `\d+` }}
// in Go: if _, err := regexp.Compile(pattern); err != nil { reject }

Try / catch

matches, err := ns.FindRE(pattern, input)
if err != nil {
    // invalid regex — fix the pattern literal, don't retry
}

Prevention

When it happens

Trigger: Only producible if the compiled-in regex were invalid (a source modification or a corrupted/forked build) — `regexp.MatchString` returns an error solely on pattern compile failure, and the input string cannot cause one. In stock Hugo, `{{ countwords .Content }}` will not hit this.

Common situations: Custom Hugo forks or patches that altered the CJK pattern; extremely unlikely toolchain/regexp package regressions. If a user reports it on an official release, it indicates a corrupted binary or a bug worth reporting upstream.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/dfef5ed84cfda522.json. Report an issue: GitHub ↗.