Q:

Python Regex to match time format works - why?

I'm writing a function that needs to match hour/minute/seconds in order to allow the user to provide a time argument on the command line in a more convenient fashion than just specifying the number of seconds.
The formats it needs to accept include:
hh:mm:ss
   mm:ss
      ss

I found a python regex that works at the following location: http://blog.yjl.im/2012/05/nargs-and-type-check-in-argparse-using.html
The regex is: '^(?:(?:(\d+):)?(\d+):)?(\d+)$'.
My question is: Why does it work? I've always thought that I was fairly good with regex, but I'm not able to fully understand this one, even with the aid of my cheatsheets.
Can somebody explain?

A:

Don't confuse with this non-captring group (?:...). It won't captures the characters which are matched by the pattern present inside this non-capturing group. () called capturing group.
Regex:   
^(?:(?:(\d+):)?(\d+):)?(\d+)$

Explanation:

(?:(\d+):)? matches the first two digits+:(ie, hh:) , captures only the digits and stored it into a group. ? makes it's presence as optional. So it may or may not occur.
(?:(?:(\d+):)?(\d+):)? And the second (\d+): matches mm: , it captures only the digits, and this must be present. So it matches mm: if hh: is present or not.? after the non-capturing group makes the whole presence as optional. So hh:mm: may or may not occur.
(\d+)$ Captures one or more digits (ie, ss)and it must be followed by a line end anchor.

