T
ToolPrime

Regex for Cron Expression

Validates standard 5-field cron expressions (minute, hour, day-of-month, month, day-of-week).

Pattern

/^(\*|[0-5]?\d)(\/\d+)?(\s+(\*|[01]?\d|2[0-3])(\/\d+)?)(\s+(\*|[0-3]?\d)(\/\d+)?)(\s+(\*|[01]?\d|1[0-2])(\/\d+)?)(\s+(\*|[0-7])(\/\d+)?)$/

Live Tester

Enter a string to test

Examples

* * * * *
0 12 * * 1
30 6 15 1 *
* * *
60 * * * *
not a cron

Pattern Breakdown

(\*|[0-5]?\d)(\/\d+)? — minute (0-59 or * with optional step)

(\*|[01]?\d|2[0-3])(\/\d+)? — hour (0-23 or *)

(\*|[0-3]?\d)(\/\d+)? — day of month (0-31 or *)

(\*|[01]?\d|1[0-2])(\/\d+)? — month (0-12 or *)

(\*|[0-7])(\/\d+)? — day of week (0-7 or *)

Code Snippets

Javascript

const regex = /^(\*|[0-5]?\d)(\/\d+)?(\s+(\*|[01]?\d|2[0-3])(\/\d+)?)(\s+(\*|[0-3]?\d)(\/\d+)?)(\s+(\*|[01]?\d|1[0-2])(\/\d+)?)(\s+(\*|[0-7])(\/\d+)?)$/;
regex.test("0 12 * * 1"); // true

Python

import re
pattern = r"^(\*|[0-5]?\d)(\/\d+)?(\s+(\*|[01]?\d|2[0-3])(\/\d+)?)(\s+(\*|[0-3]?\d)(\/\d+)?)(\s+(\*|[01]?\d|1[0-2])(\/\d+)?)(\s+(\*|[0-7])(\/\d+)?)$"
bool(re.match(pattern, "0 12 * * 1"))  # True

Php

$pattern = '/^(\*|[0-5]?\d)(\/\d+)?(\s+(\*|[01]?\d|2[0-3])(\/\d+)?)(\s+(\*|[0-3]?\d)(\/\d+)?)(\s+(\*|[01]?\d|1[0-2])(\/\d+)?)(\s+(\*|[0-7])(\/\d+)?)$/';
preg_match($pattern, "0 12 * * 1"); // 1

Related Patterns

Frequently Asked Questions

What is the regex for Cron Expression?
The regex pattern for Cron Expression is /^(\*|[0-5]?\d)(\/\d+)?(\s+(\*|[01]?\d|2[0-3])(\/\d+)?)(\s+(\*|[0-3]?\d)(\/\d+)?)(\s+(\*|[01]?\d|1[0-2])(\/\d+)?)(\s+(\*|[0-7])(\/\d+)?)$/. Validates standard 5-field cron expressions (minute, hour, day-of-month, month, day-of-week).
How do I use the Cron Expression regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the Cron Expression pattern match?
It matches strings like * * * * *; it rejects strings like * * *. See the Examples section above for the full list of matching and non-matching cases.

Related Tools