Skip to content

Instantly share code, notes, and snippets.

@besstiolle
Last active November 16, 2016 12:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save besstiolle/90c8527a85b898faf7799043b32ccb8d to your computer and use it in GitHub Desktop.
Save besstiolle/90c8527a85b898faf7799043b32ccb8d to your computer and use it in GitHub Desktop.
Standard/Default implementations of Cron expression syntax check, including special operators : * / , - and Nonstandard predefined scheduling definitions

Tldr

Here the cron regex :

(((\*)|(([0-9]|[0-5][0-9]))|(((\*|([0-9]|[0-5][0-9]))\/([0-9]|[0-5][0-9])))|(([0-9]|[0-5][0-9])(,([0-9]|[0-5][0-9]))*)|(([0-9]|[0-5][0-9])-([0-9]|[0-5][0-9])(\/([0-9]|[0-5][0-9]))?)) ((\*)|(([0-9]|[0-1][0-9]|2[0-3]))|(((\*|([0-9]|[0-1][0-9]|2[0-3]))\/([0-9]|[0-1][0-9]|2[0-3])))|(([0-9]|[0-1][0-9]|2[0-3])(,([0-9]|[0-1][0-9]|2[0-3]))*)|(([0-9]|[0-1][0-9]|2[0-3])-([0-9]|[0-1][0-9]|2[0-3])(\/([0-9]|[0-1][0-9]|2[0-3]))?)) ((\*)|(([0-9]|[0-1][0-9]|3[0-1]))|(((\*|([0-9]|[0-1][0-9]|3[0-1]))\/([0-9]|[0-1][0-9]|3[0-1])))|(([0-9]|[0-1][0-9]|3[0-1])(,([0-9]|[0-1][0-9]|3[0-1]))*)|(([0-9]|[0-1][0-9]|3[0-1])-([0-9]|[0-1][0-9]|3[0-1])(\/([0-9]|[0-1][0-9]|3[0-1]))?)) ((\*)|(([0-9]|0[0-9]|1[0-2]))|(((\*|([0-9]|0[0-9]|1[0-2]))\/([0-9]|0[0-9]|1[0-2])))|(([0-9]|0[0-9]|1[0-2])(,([0-9]|0[0-9]|1[0-2]))*)|(([0-9]|0[0-9]|1[0-2])-([0-9]|0[0-9]|1[0-2])(\/([0-9]|0[0-9]|1[0-2]))?)) ((\*)|(([0-7]))|(((\*|([0-7]))\/([0-7])))|(([0-7])(,([0-7]))*)|(([0-7])-([0-7])(\/([0-7]))?)))|(@reboot|@yearly|@annually|@monthly|@weekly|@daily|@midnight|@hourly)

Examples of valide cron

  • * * * * *
  • 0 * * * *
  • 0,15,30,45 * * * *
  • 0-30 * * * *
  • 15-45 * * * *
  • 15-45/2 * * * *
  • ... same for each fields
  • @daily
  • @Monthly
        • dec SuN

Examples of bad cron & syntaxe not managed

6 positions

  • 1 2 3 4 5 6

Offset values

  • 60 2 3 4 5
  • 1 24 3 4 5
  • 1 2 32 4 5
  • 1 2 3 13 5
  • 1 2 3 4 7

invalid char & some specials symbols

  • 1 2 a 4 5
  • 1 2 3 4 5L <- Last Friday of May
  • 1 2 3W 4 5 <- the nearest weekday to the 3th of the month
  • 1 2 3 4 5#2 <- the second Friday" of a given month
  • ? ? * * * * <- i didn't even know it exists

Limitation

  • The cron doesn't manage the 6th & 7th position, btw the classic cron doesn't manage more than 5 parts.
  • The symbol ? L W # are not managed

Need help

Feel free to improve this regex :=)

Based on : https://en.wikipedia.org/wiki/Cron, Tested on : https://regex101.com/r/cP5tX2/2 Explained on : https://www.debuggex.com/r/ZCSczLBqayVUEQEO

<?php
$re = "/(((\\*)|(([0-9]|[0-5][0-9]))|(((\\*|([0-9]|[0-5][0-9]))\\/([0-9]|[0-5][0-9])))|(([0-9]|[0-5][0-9])(,([0-9]|[0-5][0-9]))*)|(([0-9]|[0-5][0-9])-([0-9]|[0-5][0-9])(\\/([0-9]|[0-5][0-9]))?)) ((\\*)|(([0-9]|[0-1][0-9]|2[0-3]))|(((\\*|([0-9]|[0-1][0-9]|2[0-3]))\\/([0-9]|[0-1][0-9]|2[0-3])))|(([0-9]|[0-1][0-9]|2[0-3])(,([0-9]|[0-1][0-9]|2[0-3]))*)|(([0-9]|[0-1][0-9]|2[0-3])-([0-9]|[0-1][0-9]|2[0-3])(\\/([0-9]|[0-1][0-9]|2[0-3]))?)) ((\\*)|(([0-9]|[0-1][0-9]|3[0-1]))|(((\\*|([0-9]|[0-1][0-9]|3[0-1]))\\/([0-9]|[0-1][0-9]|3[0-1])))|(([0-9]|[0-1][0-9]|3[0-1])(,([0-9]|[0-1][0-9]|3[0-1]))*)|(([0-9]|[0-1][0-9]|3[0-1])-([0-9]|[0-1][0-9]|3[0-1])(\\/([0-9]|[0-1][0-9]|3[0-1]))?)) ((\\*)|(([0-9]|0[0-9]|1[0-2]))|(((\\*|([0-9]|0[0-9]|1[0-2]))\\/([0-9]|0[0-9]|1[0-2])))|(([0-9]|0[0-9]|1[0-2])(,([0-9]|0[0-9]|1[0-2]))*)|(([0-9]|0[0-9]|1[0-2])-([0-9]|0[0-9]|1[0-2])(\\/([0-9]|0[0-9]|1[0-2]))?)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)) ((\\*)|(([0-7]))|(((\\*|([0-7]))\\/([0-7])))|(([0-7])(,([0-7]))*)|(([0-7])-([0-7])(\\/([0-7]))?)|(SUN|MON|TUE|WED|THU|FRI|SAT)))|(@reboot|@yearly|@annually|@monthly|@weekly|@daily|@midnight|@hourly)/i";
$str = "* * * * *\n0 * * * *\n0,15,30,45 * * * *\n0-30 * * * *\n15-45 * * * *\n15-45/2 * * * *\n@daily\n@Monthly\n* * * dec SuN\n";
preg_match_all($re, $str, $matches);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment