Reputation: 110203
is there a way to shorten the following to find a time in the format HH:MM:SS?
'^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$'
Upvotes: 0
Views: 1633
Reputation: 303254
Assuming you don't need to do validation of the values, but just find things that look like times, how about just:
\d{1,2}:\d{2}:\d{2}
If you are searching through text to find values you don't really want to anchor at ^
and $
unless your regex flavor has those matching at the beginning and end of line, and your source data has these on their own lines.
Upvotes: 1
Reputation: 104070
If anything, it ought to be longer, since 69:69:69
does not make sense as a time.
([01]\d|2[0-3]):([0-5]\d|60):([0-5]\d|60)
allow 00-23 00-59 and 60 00-59 and 60
~~~~~~~~~~~~~~~~~~~~~~~~~
60 is useful for supporting leap seconds
If you really want your example, but shorter, you can do it:
[0-6]\d(:[0-6]\d){2}
~~ ~~~
| exactly two repetitions of preceding () block
Matches digits in many regex implementations
Upvotes: 2