I'm using some validation in Google Sheets but I need the total minutes and seconds in the following format for each dive.
E.g.
42:38 (42 minutes, 38 seconds)
62:35
85:26
I do not want hours to be an option; I only want to work with minutes and seconds, as this is then converted in the database separately.
I have seen the below on another form but have no idea where to start with regex:
^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$
This gives HH:MM:SS (with HH being optional); I have tried to have a play around with it but to no avail.
You can use
^(\d+):([0-5]?\d)$
Or, if \d is not supported as in POSIX ERE:
^([0-9]+):([0-5]?[0-9])$
See the regex demo. Details:
^ - start of string
(\d+) - Group 1: one or more digits
: - a colon
([0-5]?\d) - Group 2: a digit from 0 to 5 and then one digit
$ - end of string.
Related
I'm using some validation in Google Sheets but I need the total minutes and seconds in the following format for each dive.
E.g.
42:38 (42 minutes, 38 seconds)
62:35
85:26
I do not want hours to be an option; I only want to work with minutes and seconds, as this is then converted in the database separately.
I have seen the below on another form but have no idea where to start with regex:
^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$
This gives HH:MM:SS (with HH being optional); I have tried to have a play around with it but to no avail.
You can use
^(\d+):([0-5]?\d)$
Or, if \d is not supported as in POSIX ERE:
^([0-9]+):([0-5]?[0-9])$
See the regex demo. Details:
^ - start of string
(\d+) - Group 1: one or more digits
: - a colon
([0-5]?\d) - Group 2: a digit from 0 to 5 and then one digit
$ - end of string.
I am trying to compose a regExp that accepts HH:mm time formats, but also accepts all of the intermediate values:
e.g. all of these are accepted:
0
1
12
12:
12:3
12:30
1:
1:3
1:30
For now, I came up with this: ^([\d]{1,2}):?([\d]{1,2})?$
But this accepts any numeric 1/2 digit values for hours and minutes (e.g. 25:66 is acceptable)
So I came relatively close to my goal, but I need to filter out values x>24 from the hours, and x>60 from the minutes?
Try this:
^((?:[01][0-9]?)|(?:2[0-4]?)|(?:[3-9]))(?::((?:[0-5][0-9]?)|(?:60))|:)?$
NOTE:
This accepts 24 for HH and 60 for MM as stated in your question:
but I need to filter out values x>24 from the hours, and x>60 from the minutes?
Thus ff. are accepted:
0
1
12
12:
12:3
12:30
1:
1:3
1:30
1:60
24:60
24:00
00:60
and below are not accepted:
25:30
00:61
Regex DEMO 1
If you want to exclude 24 HH and 60 MM, try this instead:
^((?:[01]\d?)|(?:2[0-3]?))(?::|(?::([0-5][0-9]?)))?$
Regex DEMO 2
Groups (applies to both cases):
\1 = HH
\2 = MM
You are looking for
^(?:[01]\d?|2[0-3]?)(?::(?:[0-5]\d?)?)?$
See the regex demo and the regex graph:
Details:
^ - start of string
(?:[01]\d?|2[0-3]?) - either a 0 or 1 followed by an optional digit, or a 2 followed with an optional 0, 1, 2 or 3
(?::(?:[0-5]\d?)?)? - an optional sequence of patterns:
: - a colon
(?:[0-5]\d?)? - an optional sequence of patterns:
[0-5] - a digit from 1 to 5
\d? - an optional digit
$ - end of string.
I am dealing with a patient day calendar with time slots.
Each hour is divided into two equal time slots.
If the person is available for the whole 1 hour it is is denoted as 11.
If not available for the whole hour : 00.
Available for first half only : 10 .
Available for second half only : 01 .
So the entire calendar of the patient has 48 digits
But for now, just to ease things, we will assume we are looking from 12AM to 6AM only.
So the calendar will be [01]{12}
Suppose the patient is available from 12AM to 3AM and 4AM to 6AM.
Now his calendar can be marked as
111111001111
Now the doctors' calendar is also stored in the similar way
Now suppose the appointment window is fixed as 1 hour, I need to find all the doctors having a 1 hour available timeslots within patient's available timeslot
The doctors' availability outside the patient's available time doesn't matter, ie in this case 3AM to 4AM
Basically, I want to write a regex to match all those
I tried (^([01]{2})(?=(?:[01]{0,2}11))([01]{4})|^([01]{8})(?=(?:[01]{0,2}11))([01]{2})) but this is giving match for any doctor who dont have any free slot in patient's availablilty but any 1 hour free slot after 6AM also –
The following seems to validate all the schedules from 12AM to 6AM where doctor have at least an hour available in either the timeslot 12AM to 3Am or 4AM to 6AM:
^(?=(?:[01]{0,4}|[01]{8,10})11)[01]{12}$
See the online demo
^ - Start line anchor.
(?= - Open positive lookahead:
(?: - Open non-capture group to write out alternations:
[01]{0,4} - Assert position is followed by 0-4 times a zero or one.
| - Or:
[01]{8,10} - Assert position is followed by 8-10 times a zero or one.
) - Close non-capture group.
11 - Literally match "11" which represents an available hour.
) - Close positive lookahead.
[01]{12} - Match a zero or 1, exactly 12 times.
$ - End string anchor.
I am programming a function that will extract the different times from a schedule using regular expressions in Python. Here is an example of the schedule that I got from a website using BeautifulSoup:
Interactive talk with discussion17:00-18:00 Documentary ‘Occupy Gezi’
We present to you Taksim Gezi park boycott with all ways; day and
night, with good sides and bad sides18.00 - 19:00 Poet Maria van
Daalen ‘Haitian Vodoo’, poet from Querido publishers19:00
Food20:30-22:30
As shown above, the input text has starting times with and without ending times. There is also inconsistency with using either “:” or “.” when separating the hours from the minutes.
Using regex101, I have made the following (very ugly) regular expression that seems to work on all different times: \d\d[:|.]\d\d(\s*.\s*\d\d[:|.]\d\d)?
To search the text on Python I use the following code:
def extract_times(string):
list_of_times = re.findall('\d\d[:|.]\d\d(\s*.\s*\d\d[:|.]\d\d)?', string)
return list_of_times
However, when I put the example text from above in this function, it returns this:
['-18:00', ' - 19:00', '', '-22:30']
I expected something like [’17:00-18:00’], [’19:00’].
What have I done wrong?
Use this one : \d{1,2}[:.]([\d\s-]+[:.])?\d{2}}
Explanation
\d{1,2} one or two digits to match 1:00 and 01:00
[:.] to match 18:00 and 18.00
[\d\s-]+ n digit, whitespace or dash (optional)
[:.]\d{2} to match 18:00 and 18.00 (optional)
\d{2} 2 digits
In your sample text, the following will match (use full match) :
Match 1 17:00-18:00
Match 2 18.00 - 19:00
Match 3 19:00
Match 4 20:30-22:30
Demo
I want to parse a hh:mm:ss string.
A simple one is ([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d) which expects 2:3:24 or 02:03:24 string.
I want to take it a step further and pass the validation even in cases like
if you enter just 56, it should be pass, as 56 can be considered as 56 secs [SS]
if you enter 2:3 or 02:03 or 02:3 or 2:03 it should pass. 2 minutes and 3 seconds [MM:SS]
If you enter 20:30:12 pass with 20 hrs, 30 minutes and 12 secs [HH:MM:SS]
if you enter 78:12 , do not pass 78 minutes is wrong....
Basically, if one ":" is found, consider number before ":" as MM and number after ":" as SS
. If two ":" are found consider as HH:MM:SS
I came up with this pattern.
(^([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)$)|(^([0-5]?\d):([0-5]?\d)$)|(^[0-5]?\d$)
It seems to be working fine. I wanted to know any other simpler regular expression, that can do the job.
^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$
Explanation:
^ # Start of string
(?: # Try to match...
(?: # Try to match...
([01]?\d|2[0-3]): # HH:
)? # (optionally).
([0-5]?\d): # MM: (required)
)? # (entire group optional, so either HH:MM:, MM: or nothing)
([0-5]?\d) # SS (required)
$ # End of string
#Tim Pietzcker covers the OP's requirement for a HH:MM:SS parser where SS was mandatory, i.e.
HH:MM:SS
MM:SS
SS
If you permit me to deviate from the OP's requirement for a bit, and consider a case where HH is mandatory, i.e.
HH
HH:MM
HH:MM:SS
The regex I came up with was:
^([0-1]?\d|2[0-3])(?::([0-5]?\d))?(?::([0-5]?\d))?$
Let's break it down:
([0-1]?\d|2[0-3]) - matches for hours
(?::([0-5]?\d))? - optionally matches for minutes
(?::([0-5]?\d))? - optionally matches for seconds