Which regular expression would be more appropriate to validate a Cronjob expression?

Asked

Viewed 290 times

4

I’m looking to learn a little bit more about regular expressions. I thought of a good exercise that will be validating whether a cronjob expression is correct or not.

Imagem do cronjob

Therefore, the possible combinations below should return true according to the supposed regular expression.

 * * * * *

1-15 */5 0,30 * 5

0,30 * * * 1-5

Remembering that all the above numbers represent a valid Cronjob expression.

Another detail is that expression should contain 5 "members" separated by space. These members, as exemplified above, must be according to the represented in the image.

The expression can be evaluated as:

<minuto><espaço><hora><espaço><dia_do_mês><espaço><mês><espaço><dia_semana>

For those who do not understand how the cronjob works here goes a small exemeplo:

Runs from hour to hour

0 * * * * comando

Runs every hour, from 1 to 5

0 1-5 * * * comando

According to the above image, how could I create a validation for a cronjob expression?

I only need to validate the content of the time that will be executed, I do not need to validate the command that comes after the expression.

Note: I only need logic to learn regex, so any language is valid for the answer.

  • 2

    Starts here http://aurelio.net/regex/guia/

  • The comma, asterisk and bar mean what?

  • The comma and the bar are literal characters, the asterisk is a greedy quantifier. @rray

  • From what I understand, this is more of an algorithm than a REGEX, for example, if 1-15 you mean to say 1 a 15 minutes, and 0,30 0 or 30 minutes you have different sentences (0?[1-9])|(1[0-5]), 0|30. Specify the parameters :D better

  • Put 1 full sample line to make it easy....

  • Complementing the @Magichat tip, if you want to learn regular expressions the book from Aurelio is a good investment.

Show 1 more comment

1 answer

2

Boss,simplex, it’s kind of like this ???

Well, I don’t know if I understand what you’re looking for, but...

<fieldset>
<legend>Valida cron:</legend>
<h1>Formato</h1>
<div>&lt;minuto&gt;&lt;espaço&gt;&lt;hora&gt;&lt;espaço&gt;&lt;dia_do_mês&gt;&lt;espaço&gt;&lt;mês&gt;&lt;espaço&gt;&lt;dia_semana&gt;&lt;espaço&gt;&lt;comandos&gt;</div>
<h3>Exemplo</h3>
<p>* * * * * /sbin/ping -c 1 192.168.0.1 > /dev/null</p>	
<input id="jota" type="text"/><button type="button" onclick="valida()">Valida</button>
<p id="isvalid"></p>	
</fieldset>


<script>
	function valida(){
	var filter_cron = /^\s*($|#|\w+\s*=|(\*(?:\/\d+)?|(?:[0-4]?\d)(?:-(?:[0-4]?\d)(?:\/\d+)?)?(?:,(?:[0-4]?\d)(?:-(?:[0-5]?\d)(?:\/\d+)?)?)*)\s+(\*(?:\/\d+)?|(?:[01]?\d|2[0-3])(?:-(?:[01]?\d|2[0-3])(?:\/\d+)?)?(?:,(?:[01]?\d|2[0-3])(?:-(?:[01]?\d|2[0-3])(?:\/\d+)?)?)*)\s+(\*(?:\/\d+)?|(?:0?[1-9]|[12]\d|3[01])(?:-(?:0?[1-9]|[12]\d|3[01])(?:\/\d+)?)?(?:,(?:0?[1-9]|[12]\d|3[01])(?:-(?:0?[1-9]|[12]\d|3[01])(?:\/\d+)?)?)*)\s+(\*(?:\/\d+)?|(?:[1-9]|1[012])(?:-(?:[1-9]|1[012])(?:\/\d+)?)?(?:,(?:[1-9]|1[012])(?:-(?:[1-9]|1[012])(?:\/\d+)?)?)*)\s+(\*(?:\/\d+)?|(?:[0-6])(?:-(?:[0-6])(?:\/\d+)?)?(?:,(?:[0-6])(?:-(?:[0-6])(?:\/\d+)?)?)*)\s+|s+)([^\s]+)\s+(\s*.*)$/;
	if(!filter_cron.test(document.getElementById("jota").value)){
		document.getElementById("isvalid").innerHTML = "Cron inválido";
		document.getElementById("jota").onfocus = function keydown_e(){
	document.getElementById("isvalid").innerHTML = "Tente novamente você consegue ¯\\_(ツ)_/¯";
				}
	}
	if(filter_cron.test(document.getElementById("jota").value)){
		document.getElementById("isvalid").innerHTML = "Válido";}
		
	}
</script>

Reference and Guide <<<<<< Here we learn fast

Stackoverflow has everything we want, or almost everything !

  • In fact, you need to validate "", the "0-5" or "0-9", or "/9""

  • Are they just those variables or do they have more? eg: when you say "0-5" it means 0 to 5 or "0". the "-" and the "5", specify well what you want to validate...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.