Skip to content

Instantly share code, notes, and snippets.

@ritchiecarroll
Last active October 3, 2022 14:31
Show Gist options
  • Save ritchiecarroll/8b99ad99c8fe634257347f989f604199 to your computer and use it in GitHub Desktop.
Save ritchiecarroll/8b99ad99c8fe634257347f989f604199 to your computer and use it in GitHub Desktop.
Cron Validation Regular Expression

Cron Validation Regular Expressions

Examples

// C# Cron Validation Regular Expression
//                                   Dash: Minute:                                                                                                Hour:                                                                                                                      Day:                                                                                                                                   Month:                                                                                         Day of Week:
const string CronValidationRegex = @"^\-$|^(\*|(([0-5]?[0-9]|60)(,([0-5]?[0-9]|60))*)|([0-5]?[0-9]|60)\-([0-5]?[0-9]|60)(\/[0-9]+)?|\*\/[0-9]+)\s+(\*|(([0-9]|1[0-9]|2[0-4])(,([0-9]|1[0-9]|2[0-4]))*)|([0-9]|1[0-9]|2[0-4])\-([0-9]|1[0-9]|2[0-4])(\/[0-9]+)?|\*\/[0-9]+)\s+(\*|(([1-9]|[1-2][0-9]|30|31)(,([1-9]|[1-2][0-9]|30|31))*)|([1-9]|[1-2][0-9]|30|31)\-([1-9]|[1-2][0-9]|30|31)(\/[0-9]+)?|\*\/[0-9]+)\s+(\*|(([1-9]|1[0-2])(,([1-9]|1[0-2]))*)|([1-9]|1[0-2])\-([1-9]|1[0-2])(\/[0-9]+)?|\*\/[0-9]+)\s+(\*|([0-7](,[0-7])*)|[0-7]\-[0-7](\/[0-9]+)?|\*\/[0-9]+)$";
// JavaScript Cron Validation Regular Expression
//                           Dash: Minute:                                                                                                Hour:                                                                                                                      Day:                                                                                                                                   Month:                                                                                         Day of Week:
const CronValidationRegex = /^\-$|^(\*|(([0-5]?[0-9]|60)(,([0-5]?[0-9]|60))*)|([0-5]?[0-9]|60)\-([0-5]?[0-9]|60)(\/[0-9]+)?|\*\/[0-9]+)\s+(\*|(([0-9]|1[0-9]|2[0-4])(,([0-9]|1[0-9]|2[0-4]))*)|([0-9]|1[0-9]|2[0-4])\-([0-9]|1[0-9]|2[0-4])(\/[0-9]+)?|\*\/[0-9]+)\s+(\*|(([1-9]|[1-2][0-9]|30|31)(,([1-9]|[1-2][0-9]|30|31))*)|([1-9]|[1-2][0-9]|30|31)\-([1-9]|[1-2][0-9]|30|31)(\/[0-9]+)?|\*\/[0-9]+)\s+(\*|(([1-9]|1[0-2])(,([1-9]|1[0-2]))*)|([1-9]|1[0-2])\-([1-9]|1[0-2])(\/[0-9]+)?|\*\/[0-9]+)\s+(\*|([0-7](,[0-7])*)|[0-7]\-[0-7](\/[0-9]+)?|\*\/[0-9]+)$/;

Cron Validation RegEx Breakdown

General Format

  • "asterick" or "comma separated values" or "start-value dash end-value with optional slash interval" or "asterick with slash interval", e.g.:
  • * or value1[,valueN]* or value1-value2[/interval] or */interval

Minute (0-59 or 60 = 0)

(\*|(([0-5]?[0-9]|60)(,([0-5]?[0-9]|60))*)|([0-5]?[0-9]|60)\-([0-5]?[0-9]|60)(\/[0-9]+)?|\*\/[0-9]+)

Hour (0-23 or 24 = 0)

(\*|(([0-9]|1[0-9]|2[0-4])(,([0-9]|1[0-9]|2[0-4]))*)|([0-9]|1[0-9]|2[0-4])\-([0-9]|1[0-9]|2[0-4])(\/[0-9]+)?|\*\/[0-9]+)

Day (1 to 31)

(\*|(([1-9]|[1-2][0-9]|30|31)(,([1-9]|[1-2][0-9]|30|31))*)|([1-9]|[1-2][0-9]|30|31)\-([1-9]|[1-2][0-9]|30|31)(\/[0-9]+)?|\*\/[0-9]+)

Month (1 to 12)

(\*|(([1-9]|1[0-2])(,([1-9]|1[0-2]))*)|([1-9]|1[0-2])\-([1-9]|1[0-2])(\/[0-9]+)?|\*\/[0-9]+)

Day of Week (0 to 6 or 7 = 0)

(\*|([0-7](,[0-7])*)|[0-7]\-[0-7](\/[0-9]+)?|\*\/[0-9]+)

Tests

0,25,59,60 0,12,23,24 1,5,29,30,31 1,5,9,12 0,3,6,7
0-59 0-23 1-31 1-12 0-6
0-59/10 0-23/2 1-31/3 1-12/4 0-6/2
*/10 */2 */3 */4 */2

Note: In this implementation a single dash - represents a disabled cron schedule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment