Cron Expression (6-Field Quartz/Spring) Regex for Python
/^((?:[0-5]?[0-9]|\*)(?:[/,](?:[0-5]?[0-9]|\*))*) ((?:[0-5]?[0-9]|\*)(?:[/,](?:[0-5]?[0-9]|\*))*) ((?:[01]?[0-9]|2[0-3]|\*)(?:[/,](?:[01]?[0-9]|2[0-3]|\*))*) ((?:[1-9]|[12][0-9]|3[01]|[?]|L|LW|\*)(?:[/,](?:[1-9]|[12][0-9]|3[01]|[?]|L|LW|\*))*) ((?:[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\*)(?:[/,](?:[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\*))*) ((?:[0-7]|MON|TUE|WED|THU|FRI|SAT|SUN|\*|[?])(?:[-/,](?:[0-7]|MON|TUE|WED|THU|FRI|SAT|SUN|\*|[?]))*)$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching cron expression (6-field quartz/spring), 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
# Cron Expression (6-Field Quartz/Spring)
# ReDoS-safe | RegexVault — Dev & Systems > Cron
import re
cron_expression_6field_quartzspring_pattern = re.compile(r'^((?:[0-5]?[0-9]|\*)(?:[/,](?:[0-5]?[0-9]|\*))*) ((?:[0-5]?[0-9]|\*)(?:[/,](?:[0-5]?[0-9]|\*))*) ((?:[01]?[0-9]|2[0-3]|\*)(?:[/,](?:[01]?[0-9]|2[0-3]|\*))*) ((?:[1-9]|[12][0-9]|3[01]|[?]|L|LW|\*)(?:[/,](?:[1-9]|[12][0-9]|3[01]|[?]|L|LW|\*))*) ((?:[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\*)(?:[/,](?:[1-9]|1[0-2]|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|\*))*) ((?:[0-7]|MON|TUE|WED|THU|FRI|SAT|SUN|\*|[?])(?:[-/,](?:[0-7]|MON|TUE|WED|THU|FRI|SAT|SUN|\*|[?]))*)$')
def validate_cron_expression_6field_quartzspring(value: str) -> bool:
return bool(cron_expression_6field_quartzspring_pattern.fullmatch(value))
# Example
print(validate_cron_expression_6field_quartzspring("0 * * * * ?")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
0 * * * * ? | * * * * * |
0 0 12 * * ? | 0 0 25 * * ? |
0 15 10 ? * MON-FRI | 0 0 12 * * 8 |
0 0/5 14 * * ? | 60 * * * * ? |
0 0 0 L * ? | abc def ghi |
When to use this pattern
This pattern is drawn from the Dev & Systems > Cron 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
Spring's @Scheduled uses 6-field Quartz syntax. AWS CloudWatch Events uses EventBridge cron which differs from both.
Technical Notes
Quartz adds a seconds field at position 1 and supports L (last), W (weekday nearest), and # (nth weekday) modifiers. Named months and days are case-insensitive with the i flag.
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