Regex to get certains number of string - Python - regex

I need to get price of this strig "Prix\xa0de base : 26 900 euros – bonus" but there is a 0 in 'Prix\xa0de' and I don't know how to do it.
Thanks for your help!

You can use something like this:
subject = "Prix\xa0de base : 26 900 euros – bonus"
match = re.search(r"^.*:\s+([\d ]+)\s+", subject)
if match:
result = match.group(1)
else:
result = ""
result will be 26 900

If it always is followed by the word 'euros' then as simple as:
'(\d+ ?\d+) euros'
Capturing the number (or number with a space as separator) before 'euros'

Related

Regex - retrieve last n characters from a string if length exceeds maximum allowed length

I have a field Notes and allowes only 240 characters. I want a regex to retrieve the last 240 characters if the user exceeds that limit. So, if he inserts 250 chars, I want the value to be the last 240 chars.
Is this possible or I need to use string functions to retrieve last 240 characters?
Now the regex is this:
/^(.{1,240})$/
You can remove the start of string anchor ( ^ ) and try this
let str = `I have a field Notes and allowes only 240 characters. I want a regex to retrieve the last 240 characters if the user exceeds that limit. So, if he inserts 250 chars, I want the value to be the last 240 chars. Is this possible or I need to use string functions to retrieve last 240 characters?
Now the regex is this: /^(.{1,240})$/`
let last240 = (str) => str.match(/[\s\S]{1,240}$/g)
console.log(last240(str))
P.S. :- I changed regex pattern to [\s\S] to match new lines as well. if your requirement doesn't need this you just need to use .{1,240}$
How about not using regex at all?
String lastN( String v, int n ){
n < v.size() ? v.reverse().take( n ).reverse() : v
}
assert 'ef' == lastN( 'abcdef', 2 )
assert 'cdef' == lastN( 'abcdef', 4 )
assert 'abcdef' == lastN( 'abcdef', 42 )

Regex match the number in between numbers

I have a list of string containing time in the following format.
15 min 43 sec
I want to extract 43 only. I was practicing at http://regexr.com/ but could not find an answer. The answer i have come to right now is \d+\s+min+\s+(\d*)+\s+sec which is match the whole word. But it should match only 43. Looking forward for the help soon. Thanks in advance.
A rudimentary and fast solution can be... \s(\d+)\s
But try to find a better one ;)
Use lookaround:
(\d+)(?=\s+sec)
The following pattern contains two capturing groups (for minutes and seconds), and allows for an arbitrary number of whitespaces inbetween the values. If only the seconds need to be extracted, one group would suffice.
To extract the values, match against an input (using a Matcher) and read the value of the according group (matcher.group(n), where 1 is the first group):
Pattern pattern = Pattern.compile("(\\d+)\\s*min\\s*(\\d+)\\s*sec");
String[] data = {"15 min 43 sec", "15min 43sec", "15 min 43 sec"};
for (String d : data) {
Matcher matcher = pattern.matcher(d);
if (matcher.matches()) {
int minutes = Integer.parseInt(matcher.group(1));
int seconds = Integer.parseInt(matcher.group(2));
System.out.println(minutes + ":" + seconds);
} else {
System.out.println("no match: " + d);
}
}

Regex: fixed length without taking "-" into account

I am trying to formulate a regular expression to match strings with a certain length, but not counting the "-" character (but may have more than one.
For example:
123-45 12345 123456 abc 123a4... (very long string)
desired results:
123-45
12345
To match 5-digit words, allowing any number of intervening, but not leading/trailing, dashes:
(?<!-)\b(\d-*){4}\d\b(?!-)
See demo showing matching each of:
123-45 12345 1-2-3-4-5 1----2345
but none of:
1234a5 123456 1234 -12345 123-45-
You don't need a regex here. You could do the below which is in pseudo-code.
var len = str.replace("-", "").length;
if(len > 5 || len < 7){
// valid
}
Just remove every - and then check for length

decision on regular expression length

I want to accomplish the following requirements using Regex only (no C# code can be used )
• BTN length is 12 and BTN starts with 0[123456789] then it should remove one digit from left and one digit from right.
WORKING CORRECTLY
• BTN length is 12 and it’s not the case stated above then it should always return 10 right digits by removing 2 from the start. (e.g. 491234567891 should be changed to 1234567891)
NOT WORKING CORRECTLY
• BTN length is 11 and it should remove one digit from left. WORKING CORRECTLY
for length <=10 BTNs , nothing is required to be done , they would remain as it is or Regex may get failed too on them , thats acceptable .
USING SQL this can be achieved like this
case when len(BTN) = 12 and BTN like '0[123456789]%' then SUBSTRING(BTN,2,10) else RIGHT(BTN,10) end
but how to do this using Regex .
So far I have used and able to get some result correct using this regex
[0*|\d\d]*(.{10}) but by this regex I am not able to correctly remove 1st and last character of a BTN like this 015732888810 to 1573288881 as this regex returns me this 5732888810 which is wrong
code is
string s = "111112573288881,0573288881000,057328888105,005732888810,15732888815,344956345335,004171511326,01777203102,1772576210,015732888810,494956345335";
string[] arr = s.Split(',');
foreach (string ss in arr)
{
// Match mm = Regex.Match(ss, #"\b(?:00(\d{10})|0(\d{10})\d?|(\d{10}))\b");
// Match mm = Regex.Match(ss, "0*(.{10})");
// ([0*|\\d\\d]*(.{10}))|
Match mm = Regex.Match(ss, "[0*|\\d\\d]*(.{10})");
// Match mm = Regex.Match(ss, "(?(^\\d{12}$)(.^{12}$)|(.^{10}$))");
// Match mm = Regex.Match(ss, "(info)[0*|\\d\\d]*(.{10}) (?(1)[0*|\\d\\d]*(.{10})|[0*|\\d\\d]*(.{10}))");
string m = mm.Groups[1].Value;
Console.WriteLine("Original BTN :"+ ss + "\t\tModified::" + m);
}
This should work:
(0(\d{10})0|\d\d(\d{10}))
UPDATE:
(0(\d{10})0|\d{1,2}(\d{10}))
1st alternate will match 12-digits with 0 on left and 0 on right and give you only 10 in between.
2nd alternate will match 11 or 12 digits and give you the right 10.
EDIT:
The regex matches the spec, but your code doesn't read the results correctly. Try this:
Match mm = Regex.Match(ss, "(0(\\d{10})0|\\d{1,2}(\\d{10}))");
string m = mm.Groups[2].Value;
if (string.IsNullOrEmpty(m))
m = mm.Groups[3].Value;
Groups are as follows:
index 0: returns full string
index 1: returns everything inside the outer closure
index 2: returns only what matches in the closure inside the first alternate
index 3: returns only what matches in the closure inside the second alternate
NOTE: This does not deal with anything greater than 12 digits or less than 11. Those entries will either fail or return 10 digits from somewhere. If you want results for those use this:
"(0(\\d{10})0|\\d*(\\d{10}))"
You'll get rightmost 10 digits for more than 12 digits, 10 digits for 10 digits, nothing for less than 10 digits.
EDIT:
This one should cover your additional requirements from the comments:
"^(?:0|\\d*)(\\d{10})0?$"
The (?:) makes a grouping excluded from the Groups returned.
EDIT:
This one might work:
"^(?:0?|\\d*)(\\d{10})\\d?$"
(?(^\d{12}$)(?(^0[1-9])0?(?<digit>.{10})|\d*(?<digit>.{10}))|\d*(?<digit>.{10}))
which does the exact same thing as sql query + giving result in Group[1] all the time so i didn't had to change the code a bit :)

Create a regular expression for Cron statement

I need to write an regular expression that matches Cron time expressions, like these ones:
28 2 7 1 1
28 2 7 1 *
28 2 7 * *
28 2 * * *
28 * * * *
* * * * *
To validate cron expressions in general, it'll depend greatly on the specific implementation you're using
Rules
General Format
In general, most adhere to the the following format:
Field name
Mandatory?
Allowed values
Special characters
Seconds
No*
0-59
* / , -
Minutes
Yes
0-59
* / , -
Hours
Yes
0-23
* / , -
Day of month
Yes
1-31
* / , - L W
Month
Yes
1-12 or JAN-DEC
* / , -
Day of week
Yes
0-6 or SUN-SAT
* / , - L #
Year
No*
1970–2099
* / , -
*where seconds and years are non-standard and sometimes not included
Predefined Scheduling Macros:
Some flavors allow predefined time periods like this:
Entry
Equivalent to
#annually
0 0 0 1 1 * *
#yearly
0 0 0 1 1 * *
#monthly
0 0 0 1 * * *
#weekly
0 0 0 * * 0 *
#daily
0 0 0 * * * *
#hourly
0 0 * * * * *
#reboot
Intervals
Some flavors allow using the #every <duration> syntax with the following timespan units:
Unit
Definition
ns
nanosecond
us, µs
microsecond
ms
millisecond
s
second
m
minute
h
hour
Validating
Predefined Macros Regex
To validate predefined macros, you can use the following regex:
/#(annually|yearly|monthly|weekly|daily|hourly|reboot)/
Which will pass the following test cases:
#daily
#hourly
#every Regex
To validate #every durations, you can use the following regex:
/#every (\d+(ns|us|µs|ms|s|m|h))+/
Which will pass the following test cases:
#every 5s
#every 20h30m
Individual Cron Terms
Validating cron terms in the regular expression is a little trickier since there are so many variations.
Just focusing in on a single term, the following are all valid:
Two or more numbers separated by a ,
Two numbers separated by a / or -
A 1-2 digit number
A single *
To validate a single term, we can use the following regex:
/(\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*/
where \d+ just guarantees you have 1 or more numeric digits
Which will pass the following test cases:
1,2,3
1,2
1/2
1-2
1
*
Combining Cron Terms
To check the full expression, we can just make sure we have {5,7} terms with the following regex:
/(((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7}/
If we wanted to distinguish between each term, we'd need to validate numbers within a certain range:
Allowed values
Regex
0-59
[1-5]?[0-9]
0-23
2[0-3]|1[0-9]|[0-9]
1-31
3[01]|[12][0-9]|[1-9]
1-12
1[0-2]|[1-9]
0-6
[0-6]
1970–2099
19[7-9][0-9]|20[0-9][0-9]
If however, we just want to make sure something looks like a regex expression without worrying about which term is days vs. hours, the expression stays a lot cleaner, so that's what I'll go with for simplicity
Putting it all together
By combining the above statements, we can have a relatively simple sanity check that the term looks regex-y with the following expression:
/(#(annually|yearly|monthly|weekly|daily|hourly|reboot))|(#every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})/
Additional Resources
crontab.guru
regexr.com
I just finished writing one, so here is mine, hope it helps.
This should allow the * or */num and also limit the number values to their logical range (1-12 for months, 0-24 for hours, and so on)
/^(\*|([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])|\*\/([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])) (\*|([0-9]|1[0-9]|2[0-3])|\*\/([0-9]|1[0-9]|2[0-3])) (\*|([1-9]|1[0-9]|2[0-9]|3[0-1])|\*\/([1-9]|1[0-9]|2[0-9]|3[0-1])) (\*|([1-9]|1[0-2])|\*\/([1-9]|1[0-2])) (\*|([0-6])|\*\/([0-6]))$/
Took me a while :D it validates based on this documentation: http://en.wikipedia.org/wiki/Cron
However it doesn't take into account that you cannot specify day of week and day of month, ie you can specify both according to this regex. This can be used for boost::regex
(((([*])|(((([0-5])?[0-9])((-(([0-5])?[0-9])))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?))(,(((([*])|(((([0-5])?[0-9])((-(([0-5])?[0-9])))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?)))* (((([*])|(((((([0-1])?[0-9]))|(([2][0-3])))((-(((([0-1])?[0-9]))|(([2][0-3])))))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?))(,(((([*])|(((((([0-1])?[0-9]))|(([2][0-3])))((-(((([0-1])?[0-9]))|(([2][0-3])))))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?)))* (((((((([*])|(((((([1-2])?[0-9]))|(([3][0-1]))|(([1-9])))((-(((([1-2])?[0-9]))|(([3][0-1]))|(([1-9])))))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?))|(L)|(((((([1-2])?[0-9]))|(([3][0-1]))|(([1-9])))W))))(,(((((([*])|(((((([1-2])?[0-9]))|(([3][0-1]))|(([1-9])))((-(((([1-2])?[0-9]))|(([3][0-1]))|(([1-9])))))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?))|(L)|(((((([1-2])?[0-9]))|(([3][0-1]))|(([1-9])))W)))))*)|([?])) (((([*])|((((([1-9]))|(([1][0-2])))((-((([1-9]))|(([1][0-2])))))?))|((((JAN)|(FEB)|(MAR)|(APR)|(MAY)|(JUN)|(JUL)|(AUG)|(SEP)|(OKT)|(NOV)|(DEC))((-((JAN)|(FEB)|(MAR)|(APR)|(MAY)|(JUN)|(JUL)|(AUG)|(SEP)|(OKT)|(NOV)|(DEC))))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?))(,(((([*])|((((([1-9]))|(([1][0-2])))((-((([1-9]))|(([1][0-2])))))?))|((((JAN)|(FEB)|(MAR)|(APR)|(MAY)|(JUN)|(JUL)|(AUG)|(SEP)|(OKT)|(NOV)|(DEC))((-((JAN)|(FEB)|(MAR)|(APR)|(MAY)|(JUN)|(JUL)|(AUG)|(SEP)|(OKT)|(NOV)|(DEC))))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?)))* (((((((([*])|((([0-6])((-([0-6])))?))|((((SUN)|(MON)|(TUE)|(WED)|(THU)|(FRI)|(SAT))((-((SUN)|(MON)|(TUE)|(WED)|(THU)|(FRI)|(SAT))))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?))|((([0-6])L))|(W)|(([#][1-5]))))(,(((((([*])|((([0-6])((-([0-6])))?))|((((SUN)|(MON)|(TUE)|(WED)|(THU)|(FRI)|(SAT))((-((SUN)|(MON)|(TUE)|(WED)|(THU)|(FRI)|(SAT))))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?))|((([0-6])L))|(W)|(([#][1-5])))))*)|([?]))((( (((([*])|((([1-2][0-9][0-9][0-9])((-([1-2][0-9][0-9][0-9])))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?))(,(((([*])|((([1-2][0-9][0-9][0-9])((-([1-2][0-9][0-9][0-9])))?)))((/(([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?([0-9])?[0-9])))?)))*))?)
Regex for full cron expression
handles 'L' and '#' cases correctly.
day and month specific text ranges
still allow any value for number, month names and week days
/^((((\d+,)+\d+|(\d+(\/|-|#)\d+)|\d+L?|\*(\/\d+)?|L(-\d+)?|\?|[A-Z]{3}(-[A-Z]{3})?) ?){5,7})$/
Test it at: https://regexr.com/5bdes
With non capturing groups
Using (?: for hidding the groups.
^(?:(?:(?:(?:\d+,)+\d+|(?:\d+(?:\/|-|#)\d+)|\d+L?|\*(?:\/\d+)?|L(?:-\d+)?|\?|[A-Z]{3}(?:-[A-Z]{3})?) ?){5,7})$
Putting it all together
/^((((\d+,)+\d+|(\d+(\/|-|#)\d+)|\d+L?|\*(\/\d+)?|L(-\d+)?|\?|[A-Z]{3}(-[A-Z]{3})?) ?){5,7})$|(#(annually|yearly|monthly|weekly|daily|hourly|reboot))|(#every (\d+(ns|us|µs|ms|s|m|h))+)/
Some test cases
from: https://www.baeldung.com/cron-expressions#2-cron-expression-examples
# At 12:00 pm (noon) every day during the year 2017:
0 0 12 * * ? 2017
# Every 5 minutes starting at 1 pm and ending on 1:55 pm and then starting at 6 pm and ending at 6:55 pm, every day:
0 0/5 13,18 * * ?
# Every minute starting at 1 pm and ending on 1:05 pm, every day:
0 0-5 13 * * ?
# At 1:15 pm and 1:45 pm every Tuesday in the month of June:
0 15,45 13 ? 6 Tue
# At 9:30 am every Monday, Tuesday, Wednesday, Thursday, and Friday:
0 30 9 ? * MON-FRI
# At 9:30 am on 15th day of every month:
0 30 9 15 * ?
# At 6 pm on the last day of every month:
0 0 18 L * ?
# At 6 pm on the 3rd to last day of every month:
0 0 18 L-3 * ?
# At 10:30 am on the last Thursday of every month:
0 30 10 ? * 5L
# At 6 pm on the last Friday of every month during the years 2015, 2016 and 2017:
0 0 18 ? * 6L 2015-2017
# At 10 am on the third Monday of every month:
0 0 10 ? * 2#3
# At 12 am midnight on every day for five days starting on the 10th day of the month:
0 0 0 10/5 * ?
To avoid a very long line, you may split the cron string then match each field with below patterns. Value range, step and comma groups are supported!
// minute: 0-59
/^(\*|[1-5]?[0-9](-[1-5]?[0-9])?)(\/[1-9][0-9]*)?(,(\*|[1-5]?[0-9](-[1-5]?[0-9])?)(\/[1-9][0-9]*)?)*$/
// hour: 0-23
/^(\*|(1?[0-9]|2[0-3])(-(1?[0-9]|2[0-3]))?)(\/[1-9][0-9]*)?(,(\*|(1?[0-9]|2[0-3])(-(1?[0-9]|2[0-3]))?)(\/[1-9][0-9]*)?)*$/
// monthDay: 1-31
/^(\*|([1-9]|[1-2][0-9]?|3[0-1])(-([1-9]|[1-2][0-9]?|3[0-1]))?)(\/[1-9][0-9]*)?(,(\*|([1-9]|[1-2][0-9]?|3[0-1])(-([1-9]|[1-2][0-9]?|3[0-1]))?)(\/[1-9][0-9]*)?)*$/
// month: 1-12
/^(\*|([1-9]|1[0-2]?)(-([1-9]|1[0-2]?))?)(\/[1-9][0-9]*)?(,(\*|([1-9]|1[0-2]?)(-([1-9]|1[0-2]?))?)(\/[1-9][0-9]*)?)*$/
// weekDay: 0-6
/^(\*|[0-6](-[0-6])?)(\/[1-9][0-9]*)?(,(\*|[0-6](-[0-6])?)(\/[1-9][0-9]*)?)*$/
It cannot verify the range boudary, i.e. 0,*/3,6-1/20 is acceptable here
A very useful site that might help: https://crontab.guru/
Here's my solution, as I found this question and the answers didn't suit my purpose. This .NET regex conditionally parses a 5-7 part cron statement, includes (afaik) most of the special characters I've seen and will only populate the correct captures (seconds in seconds group, etc) for any length. It also doesn't try to validate the digits of the dates input, ie. you can input the 67th minute, that is more verbose and I simplified for readability. It's also not anchored anywhere and you might need some lazy matching to the end of the line to cleanly capture multiples, but i stripped that out so as not to confuse.
You'll want this as multi-line, ignore whitespace, explicit capture.
((?<seconds>\*|([0-5]?[0-9])((-|,|\/)?([0-5]?[0-9])?)+)[ \t]+)?(?#seconds)
(?<minutes>\*|([0-5]?[0-9])((-|,|\/)?([0-5]?[0-9])?)+)[ \t]+(?#minutes)
(?<hours>\*|([0-2]?[0-9])((-|,|\/)?([0-2]?[0-9])?)+)[ \t]+(?#hours)
(?<dom>\*|\?|L|([1-2]?[0-9])((-|,|\/)?([1-2]?[0-9])?)+)\s(?#dayofmonth)
(?<month>((\*)|(1?[0-9])|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))((\/|,|-|\#)((\*)|(1?[0-9])|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)))?)\s(?#month)
(?<dow>(\*|\?|L|(MON|TUE|WED|THU|FRI|SAT|SUN)|[1-7])((-|,|\/|L|W|C|\#)?((MON|TUE|WED|THU|FRI|SAT|SUN)|[1-7])?)+)\s(?#day of week)
(?(seconds)(?<year>([1-2][0-9]{3})((,|-|\/)([1-2][0-9]{3}))?)?)(?#year, cond on seconds match)
you can use the following Regular expression and use the test method on your input to check if the input is valid or not.
pattern: new RegExp(
/^((\*(?!(-))|([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])|\*\/([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?!(-)))(?!(-\d-)))(([-]{1}(\*(?!(-))|[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]))|([,]{1}(\*(?!(-))|[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|\*\/([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?!(-))))(?!(-\d-)))* ((\*(?!(-))|([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])|\*\/([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?!(-)))(?!(-\d-)))(([-]{1}(\*(?!(-))|[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]))|([,]{1}(\*(?!(-))|[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|\*\/([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(?!(-))))(?!(-\d-)))* ((\*(?!(-))|([0-9]|1[0-9]|2[0-3])|\*\/([0-9]|1[0-9]|2[0-3])(?!(-)))(?!(-\d-)))(([-]{1}(\*(?!(-))|[0-9]|1[0-9]|2[0-3]))|([,]{1}(\*(?!(-))|[0-9]|1[0-9]|2[0-3]|\*\/([0-9]|1[0-9]|2[0-3])(?!(-))))(?!(-\d-)))* ((\*(?!(-))|([1-9]|1[0-9]|2[0-9]|3[0-1])|\*\/([1-9]|1[0-9]|2[0-9]|3[0-1])(?!(-)))(?!(-\d-)))(([-]{1}(\*(?!(-))|[1-9]|1[0-9]|2[0-9]|3[0-1]))|([,]{1}(\*(?!(-))|[1-9]|1[0-9]|2[0-9]|3[0-1]|\*\/([1-9]|1[0-9]|2[0-9]|3[0-1])(?!(-))))(?!(-\d-)))* ((\*(?!(-))|([1-9]|1[0-2])|\*\/([1-9]|1[0-2])(?!(-)))(?!(-\d-)))(([-]{1}(\*(?!(-))|[1-9]|1[0-2]))|([,]{1}(\*(?!(-))|[1-9]|1[0-2]|\*\/([1-9]|1[0-2])(?!(-))))(?!(-\d-)))* ((\*(?!(-))|([0-6])|\*\/([0-6])(?!(-)))(?!(-\d-)))(([-]{1}(\*(?!(-))|[0-6]))|([,]{1}(\*(?!(-))|[0-6]|\*\/([0-6])(?!(-))))(?!(-\d-)))*$/
),
It's developed based on the below format:
'second[0-59] minute[0-59] hour[0-23] day-of-month[1-31] month[1-12] day-of-week[0-6]'
For instance, if you use something like *,*/5,5-10 5-6,10-12 20 2 * 3-4 in your input and run the pattern.test(your input variable), you will get true result.
Generally the above regular expression work properly, but if you use bigger number-small number, such as 4-3, the result will get true in the final result which is not acceptable. So, to resolve this issue, you can use the following function too for the onkeyup or onblur attributes of your input and make it equal to the following function name, like "dashCheck" to check this case as well.
dashCheck() {
let dashNumber = 0;
let text = your input name.pattern;
if (text.match(/-/g)) {
dashNumber = text.match(/-/g).length;
}
let x = -1;
for (let i = 0; i < dashNumber; ++i) {
x = text.indexOf("-", x + 1);
if (isNaN(text[x + 2]) === false && text[x + 2] !== " ") {
if (isNaN(text[x - 2]) === false) {
if (text.slice(x + 1, x + 3) >= text.slice(x - 2, x)) {
} else {
return false;
}
} else {
}
} else if (isNaN(text[x + 1]) === false) {
if (isNaN(text[x - 2]) === false && text[x - 2] !== " ") {
return false;
} else {
if (text.slice(x + 1, x + 2) >= text.slice(x - 1, x)) {
} else {
return false;
}
}
}
}
return true;
}
Now, if you use the regular expression and call the above function both, your cronjob will be perfectly checked and worked.
for the people who wanna validate allowed value
^(((([1-5]?[0-9],)+[1-5]?[0-9]|([1-5]?[0-9](\/|-)[1-5]?[0-9](\/\d+)?)|[1-5]?[0-9]|\*(\/\d+)?) )((([1-5]?[0-9],)+[1-5]?[0-9]|([1-5]?[0-9](\/|-)[1-5]?[0-9](\/\d+)?)|[1-5]?[0-9]|\*(\/\d+)?) )((((2[0-3]|1[0-9]|[0-9]),)+(2[0-3]|1[0-9]|[0-9])|((2[0-3]|1[0-9]|[0-9])(\/|-)(2[0-3]|1[0-9]|[0-9])(\/\d+)?)|(2[0-3]|1[0-9]|[0-9])|\*(\/\d+)?) )((((3[01]|[12][0-9]|[1-9]),)+(3[01]|[12][0-9]|[1-9])|((3[01]|[12][0-9]|[1-9])(\/|-)(3[01]|[12][0-9]|[1-9])(\/\d+)?)|(3[01]|[12][0-9]|[1-9])|\*(\/\d+)?) )((((1[0-2]|[1-9]),)+(3[01]|[12][0-9]|[1-9])|((1[0-2]|[1-9])(\/|-)(1[0-2]|[1-9])(\/\d+)?)|(1[0-2]|[1-9])|[A-Z]{3}|\*(\/\d+)?) )((([0-6],)+[0-6]|([0-6](\/|-)[0-6]((\/|-)\d+)?)|[0-6]|\*(\/\d+)?|[A-Z]{3}?)))$|(#(annually|yearly|monthly|weekly|daily|hourly|reboot))|(#every (\d+(ns|us|µs|ms|s|m|h))+)
#KyleMit answer's is a great place to start, but I found out that his regex is also catching some invalid use-cases like:
*-* * * * *
*1 * 1 * *
1* * 1 * *
So I updated his regex:
/(^((\*\/)?([0-5]?[0-9])((\,|\-|\/)([0-5]?[0-9]))*|\*)\s+((\*\/)?((2[0-3]|1[0-9]|[0-9]|00))((\,|\-|\/)(2[0-3]|1[0-9]|[0-9]|00))*|\*)\s+((\*\/)?([1-9]|[12][0-9]|3[01])((\,|\-|\/)([1-9]|[12][0-9]|3[01]))*|\*)\s+((\*\/)?([1-9]|1[0-2])((\,|\-|\/)([1-9]|1[0-2]))*|\*|(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|des))\s+((\*\/)?[0-6]((\,|\-|\/)[0-6])*|\*|00|(sun|mon|tue|wed|thu|fri|sat))\s*$)|#(annually|yearly|monthly|weekly|daily|hourly|reboot)/
and now it is working as expected.
It's hard to make an exact regex without knowing which program you'll use to run it, but this should work:
(28|\*) (2|\*) (7|\*) (1|\*) (1|\*)
Your current regex will only match 5 characters, since each character class can only match a single character. For each position you either want to match the number or *, with one or more spaces between each position, you can do that with the following:
(28|\*) +[2*] +[7*] +[1*] +[1*]
Note that since the first number has two digits, you need to use alternation instead of a character class.
You will probably also want to add some anchors to your regex so that it doesn't match just part of a string, if this is necessary add ^ to the beginning and $ to the end.
do the simplest thing that could possibly work
"^28 2 7 1 1$|^28 2 7 1 \*$|^28 2 7 \* \*$|^28 2 \* \* \*$|^28 \* \* \* \*$|^\* \* \* \* \*$"
http://rubular.com/r/Z0hfT5X9K8
Not sure if this can help somebody, but here are some regex to check time parameter values without the feature for writing out the names of weekdays.
//string validation
function allowed_characters($value, $mode){
switch ($mode) {
case '0': //0-59
$preg_code = "^(((([0-5]?[0-9]|60)(-([0-5]?[0-9]|60)){0,1}?)|\*)(,([0-5]?[0-9]|60)((-([0-5]?[0-9]|60)){0,1}?))*?)$";
break;
case '1': //0-23
$preg_code = "^(((([0-1]?[0-9]|2[0-4])(-([0-1]?[0-9]|2[0-4])){0,1}?)|\*)(,([0-1]?[0-9]|2[0-4])((-([0-1]?[0-9]|2[0-4])){0,1}?))*?)$";
break;
case '2': //1-31
$preg_code = "^(((([0-2]?[0-9]|3[0-1])(-([0-2]?[0-9]|3[0-1])){0,1}?)|\*)(,([0-2]?[0-9]|3[0-1])((-([0-2]?[0-9]|3[0-1])){0,1}?))*?)$";
break;
case '3': //0-12
$preg_code = "^(((([0]?[0-9]|1[0-2])(-([0]?[0-9]|1[0-2])){0,1}?)|\*)(,([0]?[0-9]|1[0-2])((-([0]?[0-9]|1[0-2])){0,1}?))*?)$";
break;
case '4': //0-6
$preg_code = "^(((([0]?[[0-7])(-([0]?[0-7])){0,1}?)|\*)(,([0]?[0-7])((-([0]?[0-7])){0,1}?))*?)$";
break;
default:
return false;
break;
}
Regex for a single expression (still simple but powerful)
NO 'L' and '#' cases currently.
NO specific number ranges
https://regex101.com/r/k7U1l7/1
^(?:(?:(?:\d+-\d+\/\d+|\d+(?:[-,\/]\d+)?|\*\/\d+)(?:,(?:\d+-\d+\/\d+|\d+(?:[-,\/]\d+)?|\*\/\d+))*)|\*)$
^(((?:[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}$