Cache-Control Directive Regex for Python
/^(?: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 Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# Cache-Control Directive
# ReDoS-safe | RegexVault — Web & Network > HTTP
import re
cachecontrol_directive_pattern = re.compile(r'^(?: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}))*$')
def validate_cachecontrol_directive(value: str) -> bool:
return bool(cachecontrol_directive_pattern.fullmatch(value))
# Example
print(validate_cachecontrol_directive("no-cache")) # TrueTest 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 Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. 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