Working on the file an need to grep the line with a time stamp in the H:MM:SS format. I tried the following egrep '[0-9]:[0-9]:[0-9]'. Didn't work for me. What am i doing wrong in regex?
3 Answers
if you want to match for
00:00:00 to 23:59:59
egrep '([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]'t
if you want to match for
12:00 pm to 12:59 am
egrep -i '((?:0[0-9]|1[012]):[0-5][0-9]:[0-5][0-9]\s?(?:a|p)m)'
- 193
- 1
- 1
- 7
-
For the 2nd one, change `[0-1][0-2]` to `(0[0-9]|1[012])` which introduces the edge cases of times > "12:00:00" – glenn jackman May 02 '14 at 20:43
-
in fact [0-1][0-2] will match for 00 to 12, since [0-1] will match for 0 or 1 not both, and [0-2] will match for 0 or 2 not both so [0-1][0-2] will match for [0-1] -> 0 [0-2] -> 0 = 00:00:00 or [0-1] -> 1 [0-2] -> 2 = 12:00:00 – Sirius_Black May 02 '14 at 21:33
-
-
@glennjackman sorry, i figured out, that mistake, Thanks for noticing – Sirius_Black May 03 '14 at 01:24
[0-9]:[0-9]:[0-9] would match H:M:S, bot not H:MM:SS.
Each set of square brackets matches a single character.
So to get something that matches H:MM:SS you could use:
[0-9]:[0-9][0-9]:[0-9][0-9]
Note, the above will NOT match H:M:S
In order to match H:MM:SS OR H:M:S OR H:M:SS OR H:MM:S use the following
[0-9]:[0-9]+:[0-9]+
+ means match the last character, 1 or more times (including the first match)
- 4,695
- 2
- 26
- 23
-
Sorry, but as far as I remember `.` matches ANY character. Not the last one again. That means, that `[0-9].` matches `5a` and `7-` and also `33`. And you don’t need to escape `:` (it has no regex meaning). Therefore: Your answer is not correct. – erik May 02 '14 at 04:41
-
My bad! You are correct sir. Editing now... Is there no repetition symbol to repeat once and only once? (I can't find one, so I'm thinking the best way is `[0-9]:[0-9][0-9]:[0-9][0-9]` – Azz May 02 '14 at 04:46
-
I don’t know of any repetition symbol (this is not vim). I use `{NUMBER}` to request a specific `NUMBER` of repetitions, see my answer. – erik May 02 '14 at 04:54
I suggest you use
egrep '[0-9]{2}:[0-9]{2}:[0-9]{2}'
where {2} means match last character at least two times and maximum two times (= exactly two times), where last character in this example was a number between 0 and 9.
If you want to search for it least one and maximum two you have to change it to
egrep '[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}'
which matches for example on 0:1:22 and 0:11:22 and 00:11:22 and 00:11:2 and so on.
The answer Sirius_Black gave is also testing on real times. My answer would match for unrealistic times like 99:99:99 – which could however be a time duration.
- 1,908
- 3
- 22
- 38