empty match in Regex - ruby

I've made the following regex with case insenstive \i
(([a-z]+(\s*-\s*[a-z]+)?)\:)\s*(\d{1,2}:\d{1,2}\s*[ap]m\s*-\s*\d{1,2}:\d{1,2}\s*[ap]m)
for this kind of input
Mon - Fri: 10:00 am - 8:00 pm
Sat: 10:00 am - 7:00 pm
Sun: 12:00 am - 5:00 pm
but I am getting these matches
Match 1
1. Mon - Fri:
2. Mon - Fri
3. - Fri
4. 10:00 am - 8:00 pm
Match 2
1. Sat:
2. Sat
3.
4. 10:00 am - 7:00 pm
Match 3
1. Sun:
2. Sun
3.
4. 12:00 am - 5:00 pm
why am I getting the 3rd (empty and "-Fri") matches?
you can play around with this in http://rubular.com/r/M4a1Jnnyrm

In order not to see that, just make the group non-capturing ((?:\s*\-\s*[A-Za-z]+)?):
(([A-Za-z]+(?:\s*\-\s*[A-Za-z]+)?)\:)\s*(\d{1,2}:\d{1,2}\s*[ap]m\s*-\s*\d{1,2}:\d{1,2}\s*[ap]m)
I'd even say that the 2nd group can also be non-capturing:
((?:[a-z]+(?:\s*-\s*[a-z]+)?)\:)\s*(\d{1,2}:\d{1,2}\s*[ap]m\s*-\s*\d{1,2}:\d{1,2}\s*[ap]m)
Now, it is much cleaner, with just 2 groups.

Because in this Mon - Fri: 10:00 am - 8:00 pm input, your optional third capturing group (\s*-\s*[a-z]+)? matches the - Fri part. For the others Sat: 10:00 am - 7:00 pm, there isn't a second part (the hyphen part) of string to capture like in the first string. So the third group returns an empty match for string 2 and 3.

The 3rd matches are captured by the group (\s*-\s*[a-z]+)
You can use a non-capturing group to omit the 3rd match group e.g.
(([a-z]+(?:\s*-\s*[a-z]+)?)\:)\s*(\d{1,2}:\d{1,2}\s*[ap]m\s*-\s*\d{1,2}:\d{1,2}\s*[ap]m)

Related

FORTRAN : maxloc

my data looks like
JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
22.60 24.60 30.60 34.60 36.20 35.70 32.10 30.20 31.40 31.60 28.00 24.80
25.40 27.60 32.40 34.60 36.50 38.10 31.70 31.40 30.30 30.20 27.00 23.90
and there are like hundreds of rows! I want to find a maximum value in each row and write it in different column next to data along with month
so my out put will be
36.20 MAY
38.10 JUN
.
.
I want to use maxloc function, but i have no idea how to use it!
Try
index = maxloc(myTable(3,:))
print *, myTable((/1,3/), index)
It should select the highest value from the third row and display the first and third value at this index.

How do I create a cron expression running in Kibana on weekday?

I would like my watcher to run from Monday to Friday only. So I'm trying to use this schedule:
"trigger": {
"schedule" : { "cron" : "0 0 0/4 * * MON-FRI" }
},
"input": {
...
However, I'm getting
Error
Watcher: [parse_exception] could not parse [cron] schedule
when I'm trying to save the watcher. Removing MON-FRI does helps but I need it.
This expression works:
0 0 0/4 ? * MON-FRI
But I'm not sure I understand why ? is required for either the day_of_week or day_of_month
Thank you!
I believe this is what you are looking for:
"0 0 0/4 ? * MON-FRI"
You can use croneval to check your cron expressions 1:
$ /usr/share/elasticsearch/bin/x-pack/croneval "0 0 0/4 ? * MON-FRI"
Valid!
Now is [Mon, 20 Aug 2018 13:32:26]
Here are the next 10 times this cron expression will trigger:
1. Mon, 20 Aug 2018 09:00:00
2. Mon, 20 Aug 2018 13:00:00
3. Mon, 20 Aug 2018 17:00:00
4. Mon, 20 Aug 2018 21:00:00
5. Tue, 21 Aug 2018 01:00:00
6. Tue, 21 Aug 2018 05:00:00
7. Tue, 21 Aug 2018 09:00:00
8. Tue, 21 Aug 2018 13:00:00
9. Tue, 21 Aug 2018 17:00:00
10. Tue, 21 Aug 2018 21:00:00
For the first expression you'll get following java exception:
java.lang.IllegalArgumentException: support for specifying both a day-of-week AND a day-of-month parameter is not implemented.
You can also use Crontab guru to get human readable descriptions like:
At every minute past every 4th hour from 0 through 23 on every day-of-week from Monday through Friday.
The question mark means 'No Specific value'. From the documentation on Quartz's website:
? (“no specific value”) - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don’t care what day of the week that happens to be, I would put “10” in the day-of-month field, and “?” in the day-of-week field. See the examples below for clarification.
http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html
I suppose since you want your schedule to run every 4 hours, mon-fri, the actual day of the month is irrelevant, so the ? specifies that. * on teh other hand would be 'all values' which would not make sense since you are specifying only mon-fri for day of the week.
Hope that helps!

Run a cron job every 5 minute only on specific hours?

I want to run some cronjob between monday-friday at 23.50 and 23.55. I tried some expressions but there is something wrong with my expression.
0 50/5 23-00 ? * MON-FRI
When I calculate next days with cronmaker it shows;
1. Tuesday, March 15, 2016 11:50 PM
2. Tuesday, March 15, 2016 11:55 PM
3. Wednesday, March 16, 2016 12:50 AM
4. Wednesday, March 16, 2016 12:55 AM
5. Wednesday, March 16, 2016 11:50 PM
Why it shows AM? I only specified 23-00 hours?
It should be 0 50/5 23 ? * MON-FRI
'-' specifies range. So, 0 50/5 23-00 ? * MON-FRI means 11pm to 12am
12:50 AM is the same as 00:50 or 24:50. 50 minutes after midnight. Since the hour is 00 and the minute 50 it matches your cron expression.
0 50/5 23-00 ? * MON-FRI
it can be write as below may fullfill your requirement
0 50/5 23 * * 1-5 ?
explain 1-5 means 1 = monday
... 5 = Friday
note: sunday = 0 or 7

Create an array of users based on each user's weekday and time attributes

I have the following code which creates an array of users depending on the "global" weekdays and times.
def current_time
# server_time = Time.now
server_time = Time.local(2013,8,19,8,30) # for time simulation
local_offset = (2 * 60 * 60)
local_time = server_time + local_offset
end
def hours_mon_to_thurs?(date)
["Monday", "Tuesday", "Wednesday", "Thursday"].include?(date.strftime("%A")) && ("08:30"..."17:00").include?(date.strftime("%H:%M"))
end
def hours_fri?(date)
["Friday"].include?(date.strftime("%A")) && ("08:30"..."15:00").include?(date.strftime("%H:%M"))
end
def hours_lunch?(date)
!("12:00"..."13:00").include?(date.strftime("%H:%M"))
end
if hours_mon_to_thurs?(current_time) && hours_lunch?(current_time)
p "[USER1, USER2]"
elsif hours_fri?(current_time) && hours_lunch?(current_time)
p "USER2"
else
p "no users"
end
This code works, but I would like to change how the array is created, so that each user has workdays and times as attributes and if they are available then they should be added to the array. Let me provide an example.
User 1 - Office Hours
Monday
08:30 - 12:00,
13:00 - 17:00
Tuesday
08:30 - 12:00,
13:00 - 17:00
Wednesday
08:30 - 12:00,
13:00 - 17:00
Thursday
08:30 - 12:00,
13:00 - 17:00
User 2 - Office Hours
Tuesday
08:30 - 12:00,
13:00 - 17:00
Thursday
08:30 - 12:00,
13:00 - 17:00
Friday
08:30 - 12:00,
13:00 - 15:00
With each user's office hours defined I want to have a similar method as in my example above. Something like this:
for each user
if current time is within user's office hours
add user to array
else if current time is not within users' office hours
array is empty
I'm not using a database at the moment and I don't know the best way to structure each user's office hours in such a way as to match if the current time is within a user's time range and then add them to an array.

algorithm for calculating a week # from a date with custom start of week? (for iCal)

I can only find algorithm for getting ISO 8601 week (week starts on a Monday).
However, the iCal spec says
A week is defined as a seven day period, starting on the day of the
week defined to be the week start (see WKST). Week number one of the
calendar year is the first week that contains at least four (4) days
in that calendar year.
Therefore, it is more complex than ISO 8601 since the start of week can be any day of the week.
Is there an algorithm to determine what is the week number of a date, with a custom start day of week?
or... is there a function in iCal4j that does this? Determine a weekno from a date?
Thanks!
p.s. Limitation: I'm using a JVM language that cannot extend a Java class, but I can invoke Java methods or instantiate Java classes.
if (input_date < firstDateOfTheYear(WKST, year))
{
return ((isLeapYear(year-1))?53:52);
}
else
{
return ((dayOfYear(input_date) - firstDateOfTheYear(WKST, year).day)/7 + 1);
}
firstDateOfTheYear returns the first calendar date given a start of week(WKST) and the year, e.g. if WKST = Thursday, year = 2012, then it returns Jan 5th.
dayOfYear returns sequencial numerical day of the year, e.g. Feb 1st = 32
Example #1: Jan 18th, 2012, start of week is Monday
dayOfYear(Jan 18th, 2012) = 18
firstDateOfTheYear(Monday, 2012) = Jan 2nd, 2012
(18 - 2)/7 + 1 = 3
Answer Week no. 3
Example #2: Jan 18th, 2012, start of week is Thursday
dayOfYear(Jan 18th, 2012) = 18
firstDateOfTheYear(Thursday, 2012) = Jan 5th, 2012
(18 - 5)/7 + 1 = 2
Answer Week no. 2
Example #3: Jan 1st, 2012, start of week is Monday
firstDateOfTheYear(Monday, 2012) = Jan 2nd, 2012
IsLeapYear(2012-1) = false
Jan 1st, 2012 < Jan 2nd, 2012
Answer Week no. 52
Let daysInFirstWeek be the number of days on the first week of the year that are in January. Week starts on a WKST day. (e.g. if Jan 1st is a WKST day, return 7)
Set dayOfYear to the n-th days of the input date's year (e.g. Feb 1st = 32)
If dayOfYear is less than or equal to daysInFirstWeek
3.1. if daysInFirstWeek is greater than or equal to 4, weekNo is 1, skip to step 5.
3.2. Let daysInFirstWeekOfLastYear be the number of days on the first week of the previous year that are in January. Week starts on a WKST day.
3.3. if daysInFirstWeekOfLastYear is 4 or last year is Leap year and daysInFirstWeekOfLastYear is 5, weekNo is 53, otherwise weekNo is 52, skip to step 5.
Set weekNo to ceiling((dayOfYear - daysInFirstWeek) / 7)
4.1. if daysInFirstWeek greater than or equal to 4, increment weekNo by 1
4.2. if daysInFirstWeek equal 53 and count of days on the first week (starting from WKST) of January in the year of inputDate's year + 1 is greater than or equal to 4, set weekNo to 1
return weekNo

Resources