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 need to create regex that allows below time values:
single digit or double digits and optionally second.
All possible combination for single digit of Hour/Minute/Second:
hh:mm:ss AM/PM
h:m:s AM/PM
hh:m:s AM/PM
h:mm:s AM/PM
h:m:ss
Accepted values are:
11:12:59 AM
1:2:3 PM
1:22:30 pm
01:2:00 PM
1:3 PM
I tried some possible combination from,
https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s06.html like:
^([0-1]?\d|2[0-3])(?::([0-5]?\d))?(?::([0-5]?\d))?( )[AaPp][Mm]
But it doesn't work as expected above.
Can you please correct me on above sample or some other regex that allows above possible values.
The regex in the question doesn't make the am/pm optional.
And the minutes/seconds can be 1 group that occurs 1 to 2 times.
^(?:[01]?[0-9]|2[0-3])(?:[:][0-5]?[0-9]){1,2}(?:[ ][APap][Mm])?$
Test on regex101 here
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 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