56

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

  1. if you enter just 56, it should be pass, as 56 can be considered as 56 secs [SS]
  2. if you enter 2:3 or 02:03 or 02:3 or 2:03 it should pass. 2 minutes and 3 seconds [MM:SS]
  3. If you enter 20:30:12 pass with 20 hrs, 30 minutes and 12 secs [HH:MM:SS]
  4. 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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
user418836
  • 847
  • 3
  • 8
  • 19

2 Answers2

153
^(?:(?:([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
  • 328,213
  • 58
  • 503
  • 561
  • 2
    Thanks very much. This is much simpler and cool. I want to learn regex. Please point me in right direction. How do I learn regex. All I currently know is basic syntax (Quick tutorial kind of stuff) – user418836 Nov 29 '11 at 23:28
  • 8
    @user418836: Check out www.regular-expressions.info – Tim Pietzcker Nov 30 '11 at 08:13
  • how can i use this in posix c for the regcomp function. i get an unkown excape sequence at '\d' – Aditya P Feb 24 '12 at 09:35
  • 2
    @AdityaGameProgrammer: Try `[0-9]` instead of `\d`. – Tim Pietzcker Feb 24 '12 at 21:16
  • Dint work .http://stackoverflow.com/questions/9428586/c-posix-regex-to-validate-input-hhmmss-time-string – Aditya P Feb 28 '12 at 11:53
  • 5
    Wonderful explanation. If you also want to match decimal seconds, it's easy to add that as well: `^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)(?:\.(\d+))?$`, try it [here](http://pythex.org/?regex=%5E(%3F%3A(%3F%3A(%5B01%5D%3F%5Cd%7C2%5B0-3%5D)%3A)%3F(%5B0-5%5D%3F%5Cd)%3A)%3F(%5B0-5%5D%3F%5Cd)(%3F%3A%5C.(%5Cd%2B))%3F%24&test_string=10%3A34%3A23.414&ignorecase=0&multiline=0&dotall=0&verbose=0) – bcattle Mar 07 '17 at 09:55
  • How to use ^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$ pattern in ng-pattern – Rigin Oommen Aug 17 '17 at 14:18
  • 2
    @bcattle you probably meant `^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)(\.(\d{1,9}))?$` - the milliseconds are usually presented only after a point ... if at all – Yordan Georgiev Mar 02 '18 at 09:27
13

@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
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75