In fact, this corresponds to 23:10:7 and 23:10:6 , but since you are not using the end of the line metacharacter $ at the end of the line, it will handle everything that follows.
egrep '^(2[0-3]|1[0-9]|0[0-9]|[^0-9][0-9]):([0-5][0-9]|[0-9]):([0-5][0-9]|[0-9])$' test.txt
In other words, you should only allow [0-9] at the end of the line if the matching digit is the last in the line, that is, if it is followed by $ .
Another option is to make the last digit be 0-padded if it is less than 10, i.e. instead of [0-9] use 0[0-9] . This will correspond to 23:10:07 , but not 23:10:7 . This is the same as what you already have for part of the watch.
source share