Regex pattern for EXACTLY HH:MM:SS time String
22,480
Solution 1
Your regex would be,
(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)
This will match both "20:30:30" and "2020-05-29 20:30:30 -0600".
If you want to only match Strings that are exclusively 24-hour times, use the following:
^(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)$
This will match only "20:30:30" and not "2020-05-29 20:30:30 -0600".
Java regex would be,
(?:[01]\\d|2[0123]):(?:[012345]\\d):(?:[012345]\\d)
And for exclusively 24-hour Strings,
^(?:[01]\\d|2[0123]):(?:[012345]\\d):(?:[012345]\\d)$
Solution 2
Give this a try:
^([0-1]\d|2[0-3]):([0-5]\d):([0-5]\d)$
Solution 3
The pattern you want is:
(([0-1]?[0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]
which is for HH:MM:SS
24 hour format.
Comments
-
ahmednabil88 almost 4 years
I want to validate sting time format in EXACTLY
hh:mm:ss
String.
I mean by EXACTLY that
Each of hours / minutes / seconds MUST be 2 digits
Also Accept only logical values like- hours [ from 00 to 23 ]
- minutes [ from 00 to 59 ]
- seconds [ from 00 to 59 ]
When i checked Regex pattern for HH:MM:SS time string
The answer accepthh:mm:ss
string but also accepts cases like2:3:24
Thanks in advance