0

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?

Serg12
  • 101
  • 1
  • Can you provide a sample of the file you are running this on, what you expect to get, and what you are getting instead? It is much easier to diagnose a regex if we can see the data and have an understanding of your expectations. – MaQleod May 02 '14 at 01:38

3 Answers3

3

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)'
Sirius_Black
  • 193
  • 1
  • 1
  • 7
0

[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)

Azz
  • 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
0

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.

erik
  • 1,908
  • 3
  • 22
  • 38