Cron Expression (6-Field Quartz/Spring) Regex for PHP
/^((?:[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 PHP. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.
Php Implementation
<?php
// Cron Expression (6-Field Quartz/Spring)
// ReDoS-safe | RegexVault — Dev & Systems > Cron
define('CRON_EXPRESSION_6FIELD_QUARTZSPRING_PATTERN', '/^((?:[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|\*|[?]))*)$/');
function validate_cron_expression_6field_quartzspring(string $input): bool {
return (bool) preg_match(CRON_EXPRESSION_6FIELD_QUARTZSPRING_PATTERN, $input);
}
// Example
var_dump(validate_cron_expression_6field_quartzspring("0 * * * * ?")); // bool(true)Test 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 PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. 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