Regex for Cron expression (5-field)
Standard 5-field cron: minute hour day month weekday.
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)$
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
Once you've validated a candidate value matches this pattern, you'll typically use it inside your application code. Each language has its own regex syntax:
- JavaScript:
new RegExp(pattern).test(value)or/pattern/.test(value) - Python:
re.match(pattern, value)with raw strings:r"pattern" - Java:
Pattern.compile(pattern).matcher(value).matches() - C# / .NET:
Regex.IsMatch(value, pattern) - Go:
regexp.MustCompile(pattern).MatchString(value)— Go uses RE2 so some advanced features aren't available - Ruby:
value =~ /pattern/orpattern.match?(value) - PHP:
preg_match('/pattern/', $value)
The explainer's Code tab generates these for any pattern you paste — including the right escaping and idioms for each language.
Common pitfalls
- Anchors matter. The pattern starts with
^and ends with$— it expects the entire input to match. To find this pattern inside a longer text, remove the anchors and use the/gflag for multiple matches. - Case sensitivity. Letter ranges like
[A-Z]only match uppercase. Use theiflag or[A-Za-z]for case-insensitive matching. - Escape user input. If you're building a regex from a string variable, escape regex metacharacters first to avoid bugs or injection — use
RegExp.escape-equivalents in your language. - Performance. For this specific pattern the risk is low, but be cautious of nested quantifiers when adapting it — they can cause exponential backtracking on adversarial input.
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.