3

Possible Duplicate:
Validating Crontab Entries w/ PHP

I have search everywhere, and my Regex is poor, so i seek assistance.

I am building a web based CRON tool, and the form can and will only pass the following to the processing script.'0-99' ',' '*' '-' '/'

I wan to validate the input with a simple preg_match in PHP, but cannot seem to come up with the correct regex. Can some one help out? Also, i will validate each item in the cron i.e. NOT * * * * * but rather the minutes value, then the hours value etc.. This allows more specific errors to be sent back.

examples of the passed values would be

*
0,10
*/5
1,3,5
1-5

here is a poor attempt just to match digits and the * /[0-9*]/ Any assistance would be great.

Regards

Community
  • 1
  • 1
Stacky
  • 106
  • 1
  • 8
  • "you will validate" - do you mean the regex should do the validation, or you do that after the regex has matched? If it's the former, then we need more info on what is allowed/forbidden. Your examples are not sufficient to deduce this. Also, do you mean by `0-99` that that's the range of allowed numbers and therefore `100` should not be matched? – Tim Pietzcker Oct 08 '12 at 15:49
  • Right, i've been over the Validating Crontab Entries w/ PHP. link and it's just not getting me what i need. – Stacky Oct 08 '12 at 15:51
  • In terms of validating, what i mean is to match using regex on the string passed from a form. The string will be per any of the examples above and in fact on double checking, the digits to be matched will of course only be from 0 to a maximum number of 31 per days in the month – Stacky Oct 08 '12 at 15:53

1 Answers1

5

Let's see:

$pattern = '/^(?:[1-9]?\d|\*)(?:(?:[\/-][1-9]?\d)|(?:,[1-9]?\d)+)?$/';

This pattern will allow you to match a reasonable subset of valid CRON sentences. It sure does match every single one of your examples. The exact pattern depends on the grammar you define, though.

EDIT
Forgot to mention that regex matching alone won't cut it. What it can do, is to check whether or not the input is correct in terms of (your simplified) syntax. Other than that you will need to validate the input with regards to semantics (e.g. hours 0-23; days 1-30/1 etc.).

aefxx
  • 24,835
  • 6
  • 45
  • 55
  • yup, this pattern seems to work very well for individual CRON patterns per the samples i gave. Good one! – Stacky Oct 08 '12 at 18:58
  • ^(((?:[1-9]?\d|\*)(\/[0-9]{1,2}){0,1}\s){4}(?:([1-9]?\d|\*)(\/[0-9]{1,2}){0,1}|(MON)|(TUE)|(WED)|(THU)|(FRI)|(SAT)|(SUN))){1}$ – Sheshgiri Anvekar Feb 22 '23 at 12:53