REGEXVAULTv2.0
Web & Network/HTTP
Verified Safe

Cache-Control Directive Regex for Go

/^(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10})(?:\s*,\s*(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10}))*$/i

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching cache-control directive, ported and verified for Go. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.

Go Implementation

Go
// Cache-Control Directive
// ReDoS-safe | RegexVault — Web & Network > HTTP

package validation

import "regexp"

var cachecontrolDirectiveRe = regexp.MustCompile(`^(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10})(?:\s*,\s*(?:no-cache|no-store|no-transform|public|private|must-revalidate|proxy-revalidate|only-if-cached|immutable|max-age=[0-9]{1,10}|s-maxage=[0-9]{1,10}|max-stale(?:=[0-9]{1,10})?|min-fresh=[0-9]{1,10}|stale-while-revalidate=[0-9]{1,10}|stale-if-error=[0-9]{1,10}))*$`)

func ValidateCachecontrolDirective(s string) bool {
    return cachecontrolDirectiveRe.MatchString(s)
}

// Example
// fmt.Println(ValidateCachecontrolDirective("no-cache")) // true

Test Cases

Matches (Valid)
Rejects (Invalid)
no-cachemax-age=abc
max-age=3600invalid-directive
public, max-age=86400cache-control
no-store, no-cachemax-age=-1
max-age=0, must-revalidatemax-age=

When to use this pattern

This pattern is drawn from the Web & Network > HTTP category and carries a ReDoS-safe certification. That matters for Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

Cache-Control directives are space-tolerant after commas but must not have spaces around = signs. The pattern enforces no spaces around =.

Technical Notes

max-age values are in seconds. max-age=0 combined with must-revalidate is equivalent to no-cache for strict clients. Stale-while-revalidate is from RFC 5861.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern