Tried to create a (simplified) regex for cron patterns for myself and came up with the following one:
^(((\d+(,\d+)*)|(\d+-\d+)|(\*(\/\d+)?))(\s+(\d+(,\d+)*)|(\d+-\d+)|(\*(\/\d+)?)){4})$ This is generated by
var variant1 = "\\d+(,\\d+)*"; var variant2 = "\\d+-\\d+"; var variant3 = "\\*(\\/\\d+)?"; var variants = "(" + variant1 + ")|(" + variant2 + ")|(" + variant3 + ")"; var regex_cron = "^((" + variants + ")(\\s+" + variants + "){4})$"; For a lot of cron patterns this is working just fine:
* 4 45 6 7 5 4 45 6 7 */5 3,4 45,6,8 6 5 But if I use an asterisk in "group" 2 or higher, it fails:
4 * * * * I know that there are a couple of regular expressions for recognizing cron patterns available on the internet, but I really would like to know where I went wrong during creating the regex.