Regular Expression For Cron Time Expressions

Any task scheduler or automation system will have Cron syntax. This simple set of rules allows users to define when a task should be run. Oftentimes, Cron will follow the Unix standard for specifying the time by minute, hour, day, and month. This post will help anyone needing to match Cron Time Expressions using regular expressions.

/(((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7}/

Matches:

  • 0 * 14 * *
  • 45 23 * * 6 /home/oracle/scripts/export_dump.sh
  • 1 0 * * * printf “” > /var/log/apache/error_log

This regex matches valid cron time expressions with predefined scheduling macros and @every durations.

/(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})/

See Also: