Cron Expression (6-Field Quartz/Spring) Regex for Java
/^((?:[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 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
// Cron Expression (6-Field Quartz/Spring)
// ReDoS-safe | RegexVault — Dev & Systems > Cron
import java.util.regex.Pattern;
public class CronExpression6fieldQuartzspringValidator {
private static final Pattern PATTERN =
Pattern.compile("^((?:[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|\\*|[?]))*)$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("0 * * * * ?")); // 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 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
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