Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging
Pattern

Regex for Cron expression (5-field)

Standard 5-field cron: minute hour day month weekday.

The Cron expression (5-field) regex is ^(\*|[0-5]?\d|\*\/\d+|[0-5]?\d-[0-5]?\d|[0-5]?\d(,[0-5]?\d)+)\s+(\*|1?\d|2[0-3]|\*\/\d+|\d+-\d+|\d+(,\d+)+)\s+(\*|[1-9]|[12]\d|3[01]|\*\/\d+|\d+-\d+|\?|L|W)\s+(\*|[1-9]|1[0-2]|\*\/\d+|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\s+(\*|[0-6]|\*\/\d+|MON|TUE|WED|THU|FRI|SAT|SUN|\?|L)$ — copy it below, or open it in the explainer for a token-by-token breakdown.

The pattern

^(\*|[0-5]?\d|\*\/\d+|[0-5]?\d-[0-5]?\d|[0-5]?\d(,[0-5]?\d)+)\s+(\*|1?\d|2[0-3]|\*\/\d+|\d+-\d+|\d+(,\d+)+)\s+(\*|[1-9]|[12]\d|3[01]|\*\/\d+|\d+-\d+|\?|L|W)\s+(\*|[1-9]|1[0-2]|\*\/\d+|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\s+(\*|[0-6]|\*\/\d+|MON|TUE|WED|THU|FRI|SAT|SUN|\?|L)$
Open in explainer → Download cheat sheet ↓

Token-by-token breakdown

Every part of the pattern, left to right:

TokenMeaning
^start of string (or line in multiline mode)
(start of a capturing group
\*literal “*”
|OR — try the alternative
[0-5]?optional (zero or one): any of: 0–5
\dany digit (0–9)
|OR — try the alternative
\*literal “*”
\/literal “/”
\d+one or more: digit (0–9)
|OR — try the alternative
[0-5]?optional (zero or one): any of: 0–5
\dany digit (0–9)
-literal text “-”
[0-5]?optional (zero or one): any of: 0–5
\dany digit (0–9)
|OR — try the alternative
[0-5]?optional (zero or one): any of: 0–5
\dany digit (0–9)
(start of a capturing group
,literal text “,”
[0-5]?optional (zero or one): any of: 0–5
\dany digit (0–9)
)+end of group, one or more
)end of group
\s+one or more: whitespace
(start of a capturing group
\*literal “*”
|OR — try the alternative
1?optional (zero or one): the literal “1”
\dany digit (0–9)
|OR — try the alternative
2literal text “2”
[0-3]any of: 0–3
|OR — try the alternative
\*literal “*”
\/literal “/”
\d+one or more: digit (0–9)
|OR — try the alternative
\d+one or more: digit (0–9)
-literal text “-”
\d+one or more: digit (0–9)
|OR — try the alternative
\d+one or more: digit (0–9)
(start of a capturing group
,literal text “,”
\d+one or more: digit (0–9)
)+end of group, one or more
)end of group
\s+one or more: whitespace
(start of a capturing group
\*literal “*”
|OR — try the alternative
[1-9]any of: 1–9
|OR — try the alternative
[12]any of: “1”, “2”
\dany digit (0–9)
|OR — try the alternative
3literal text “3”
[01]any of: “0”, “1”
|OR — try the alternative
\*literal “*”
\/literal “/”
\d+one or more: digit (0–9)
|OR — try the alternative
\d+one or more: digit (0–9)
-literal text “-”
\d+one or more: digit (0–9)
|OR — try the alternative
\?literal “?”
|OR — try the alternative
Lliteral text “L”
|OR — try the alternative
Wliteral text “W”
)end of group
\s+one or more: whitespace
(start of a capturing group
\*literal “*”
|OR — try the alternative
[1-9]any of: 1–9
|OR — try the alternative
1literal text “1”
[0-2]any of: 0–2
|OR — try the alternative
\*literal “*”
\/literal “/”
\d+one or more: digit (0–9)
|OR — try the alternative
JANliteral text “JAN”
|OR — try the alternative
FEBliteral text “FEB”
|OR — try the alternative
MARliteral text “MAR”
|OR — try the alternative
APRliteral text “APR”
|OR — try the alternative
MAYliteral text “MAY”
|OR — try the alternative
JUNliteral text “JUN”
|OR — try the alternative
JULliteral text “JUL”
|OR — try the alternative
AUGliteral text “AUG”
|OR — try the alternative
SEPliteral text “SEP”
|OR — try the alternative
OCTliteral text “OCT”
|OR — try the alternative
NOVliteral text “NOV”
|OR — try the alternative
DECliteral text “DEC”
)end of group
\s+one or more: whitespace
(start of a capturing group
\*literal “*”
|OR — try the alternative
[0-6]any of: 0–6
|OR — try the alternative
\*literal “*”
\/literal “/”
\d+one or more: digit (0–9)
|OR — try the alternative
MONliteral text “MON”
|OR — try the alternative
TUEliteral text “TUE”
|OR — try the alternative
WEDliteral text “WED”
|OR — try the alternative
THUliteral text “THU”
|OR — try the alternative
FRIliteral text “FRI”
|OR — try the alternative
SATliteral text “SAT”
|OR — try the alternative
SUNliteral text “SUN”
|OR — try the alternative
\?literal “?”
|OR — try the alternative
Lliteral text “L”
)end of group
$end of string (or line in multiline mode)

About this pattern

Date and time formats vary enormously across regions and systems. The regex here validates structure only — a date like 2024-02-30 would pass format validation but isn't a real date. After regex passes, parse with your language's date library to confirm semantic validity.

Quick usage in different languages

This exact pattern — with the correct escaping and idioms for each language:

  • JavaScript: /^(\*|[0-5]?\d|\*\\/\d+|[0-5]?\d-[0-5]?\d|[0-5]?\d(,[0-5]?\d)+)\s+(\*|1?\d|2[0-3]|\*\\/\d+|\d+-\d+|\d+(,\d+)+)\s+(\*|[1-9]|[12]\d|3[01]|\*\\/\d+|\d+-\d+|\?|L|W)\s+(\*|[1-9]|1[0-2]|\*\\/\d+|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\s+(\*|[0-6]|\*\\/\d+|MON|TUE|WED|THU|FRI|SAT|SUN|\?|L)$/.test(value)
  • Python: re.match(r"^(\*|[0-5]?\d|\*\/\d+|[0-5]?\d-[0-5]?\d|[0-5]?\d(,[0-5]?\d)+)\s+(\*|1?\d|2[0-3]|\*\/\d+|\d+-\d+|\d+(,\d+)+)\s+(\*|[1-9]|[12]\d|3[01]|\*\/\d+|\d+-\d+|\?|L|W)\s+(\*|[1-9]|1[0-2]|\*\/\d+|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\s+(\*|[0-6]|\*\/\d+|MON|TUE|WED|THU|FRI|SAT|SUN|\?|L)$", value)
  • Java: Pattern.compile("^(\\*|[0-5]?\\d|\\*\\/\\d+|[0-5]?\\d-[0-5]?\\d|[0-5]?\\d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3[01]|\*\\/\d+|\d+-\d+|\?|L|W)\s+(\*|[1-9]|1[0-2]|\*\\/\d+|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\s+(\*|[0-6]|\*\\/\d+|MON|TUE|WED|THU|FRI|SAT|SUN|\?|L)$/.match?(value)
  • PHP: preg_match('~^(\*|[0-5]?\d|\*\/\d+|[0-5]?\d-[0-5]?\d|[0-5]?\d(,[0-5]?\d)+)\s+(\*|1?\d|2[0-3]|\*\/\d+|\d+-\d+|\d+(,\d+)+)\s+(\*|[1-9]|[12]\d|3[01]|\*\/\d+|\d+-\d+|\?|L|W)\s+(\*|[1-9]|1[0-2]|\*\/\d+|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\s+(\*|[0-6]|\*\/\d+|MON|TUE|WED|THU|FRI|SAT|SUN|\?|L)$~', $value)

The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.

Common pitfalls

  • Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
  • Watch for backtracking. This pattern nests quantifiers; on adversarial input that can cause exponential backtracking (ReDoS). Test with long non-matching strings, or use an RE2-based engine.
  • Escape it correctly per language. In Java and JavaScript strings each backslash must be doubled (\\d); in Python, Go, and C# use raw/verbatim strings so the backslashes survive.
  • Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the cron expression (5-field) against a source of truth (database, API, or checksum) where it matters.

Standards & sources

This pattern is based on the following authoritative specification(s) and issuing authorities. Formats can change — always confirm against the primary source.

Related patterns

More patterns in the Date & time category:

See also

Browse all 300 patterns in the library, or open this regex in the interactive explainer to see a token-by-token breakdown, test against custom input, and generate code in seven languages.


← Back to all patterns