Cron Expression (6-Field Quartz/Spring) Regex for Go
/^((?:[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 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
// Cron Expression (6-Field Quartz/Spring)
// ReDoS-safe | RegexVault — Dev & Systems > Cron
package validation
import "regexp"
var cronExpression6fieldQuartzspringRe = regexp.MustCompile(`^((?:[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|\*|[?]))*)$`)
func ValidateCronExpression6fieldQuartzspring(s string) bool {
return cronExpression6fieldQuartzspringRe.MatchString(s)
}
// Example
// fmt.Println(ValidateCronExpression6fieldQuartzspring("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 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
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