Regex to Match Cron Expression Format

Validate standard 5-field cron expression format with fields for minute, hour, day, month, and weekday. Supports wildcards, ranges, and steps. Free regex tester.

Regular Expression

/^(?:[0-9*,/\-]+\s+){4}[0-9*,/\-]+$/

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
(?:Start of non-capturing group
[0-9*,/\-]Character class — matches any one of: 0-9*,/\-
+Matches the preceding element one or more times (greedy)
\sMatches any whitespace character (space, tab, newline)
+Matches the preceding element one or more times (greedy)
)End of group
{4}Matches exactly 4 times
[0-9*,/\-]Character class — matches any one of: 0-9*,/\-
+Matches the preceding element one or more times (greedy)
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates the structural format of standard 5-field cron expressions. Here is the token-by-token breakdown:

^ — Anchors the match at the start of the string.

(?:[0-9*,/-]+\s+){4} — A non-capturing group that matches exactly four cron fields, each followed by whitespace. Each field consists of one or more characters from the cron character set: digits (0-9) for specific values, asterisk (*) for wildcards meaning every value, comma (,) for lists of values, forward slash (/) for step values, and hyphen (-) for ranges. The \s+ matches one or more spaces separating the fields. The {4} quantifier requires exactly four such field-plus-space sequences.

[0-9*,/-]+ — Matches the fifth and final cron field (day of week) using the same character set but without trailing whitespace.

$ — Anchors the match at the end of the string.

No flags are used since this validates a single cron expression string.

The five fields in a standard cron expression represent: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-7). Examples include: * * * * * (every minute), 0 9 * * 1-5 (9 AM on weekdays), */5 * * * * (every 5 minutes), and 0 0 1 1 * (midnight on January 1st).

This pattern validates the structural format but does not validate the semantic correctness of field values. It is useful for input validation in cron builders, scheduling interfaces, and configuration file parsers.

Example Test Strings

InputExpected
* * * * *Match
0 9 * * 1-5Match
*/5 * * * *Match
not a cronNo Match
* * *No Match
0,30 * * * *Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 34 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →