Cache-Control Directive Regex for Java
/^(?: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}))*$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching cache-control directive, ported and verified for Java. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.
Java Implementation
// Cache-Control Directive
// ReDoS-safe | RegexVault — Web & Network > HTTP
import java.util.regex.Pattern;
public class CachecontrolDirectiveValidator {
private static final Pattern PATTERN =
Pattern.compile("^(?: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}))*$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("no-cache")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
no-cache | max-age=abc |
max-age=3600 | invalid-directive |
public, max-age=86400 | cache-control |
no-store, no-cache | max-age=-1 |
max-age=0, must-revalidate | max-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 Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. 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