Dren
Dren

Reputation: 319

regex to match time with seconds

I have log entry lines like below;

ng with SeqID 339708.08/17/2016 16:27:18.946 - JobID 33720: Location 15531 applied clip data successfully. Updating OCAMT_GM_Sent

or

Retry count 008/17/2016 16:27:15.227 - JobID 33480: Location 15664 applied clip data successfully. Updating OCAMT_GM_Sent

I am trying to capture the time string using regex. So far I came up with below;

[0-9]{2}+[:]+[0-9]{2}+[:]+[0-9]{2}+[.]+[\d]+$

But is there a another approach for better accuracy on capturing the said time string and putting it on a variable on bash.

Upvotes: 1

Views: 661

Answers (3)

Roberto Reale
Roberto Reale

Reputation: 4317

Step 1: simplifying the original regex

Your regex can be simplified to

\d{2}:\d{2}:\d{2}\.\d+

This is because [0-9] is equivalent to \d, and the [] pair is redundant around a single character. As to the literal dot between the seconds and their decimal part, it is advisable to escape it with \.

Step 2: capturing the seconds part

If you want to match the seconds part, a group-capturing pair of parentheses can be added.

\d{2}:\d{2}:(\d{2}(?:\.\d+)?)

As a bonus, the matching here is a little more general, since we are not assuming anymore that a decimal-seconds part should be present (hence the inner non-capturing group, which is signalled by the ?: marker).

Note: I am assuming a PCRE flavour throughout.

Upvotes: 1

pakistanprogrammerclub
pakistanprogrammerclub

Reputation: 827

Assuming line is like "... time - JobID ..." you can use

pat=" ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}) - JobID "
[[ $line =~ $pat ]] && time=${BASH_REMATCH[1]}

Upvotes: 0

engineer14
engineer14

Reputation: 617

.*:([\d.]+)

Assuming that the colon succeeded by a combination of numbers and dots is going to be your sequence, then this will work.

Note, this assumes that the last sequence of colon followed by numbers and periods will be your seconds.

lets make it more robust:

.*? \d+:\d+:([\d\.]+)

Now, this does a non greedy match of everything, because we want to make sure to stop at the start of your time string. and then matches the time string while capturing the seconds.

How about take it one step further and make it more fool proof: .*? \d+:\d+:(\d+(\.\d+)?)

Now, this matches your seconds and has the optional fractional seconds there, and wouldn't match some errant number with multiple dots

Upvotes: 0

Related Questions