Reputation: 203
I was working on extracting the log lines of interest from a huge log file. Note that I'm not familiar with python at all.
The log line all starts from a timestamp, like
09:00:00.648711172 [Info ] [....]
And I want to match it with a specific period of time, say from 09:00 to 09:30, I came up with the following solution:
r'^(09:[012][0-9]|30)
The problem is this cannot apply to a generic case, say we're given a start_time
and an end_time
, how can I do the match?
Upvotes: 1
Views: 832
Reputation: 1342
Regex that matches any one entry of date from 09:00:00 to 09:30:00 and doesn't match anything else:
log_regex = re.compile(r'09:(([0-2]/d{1}):([0-5]/d{1}|60))|30:00')
You want to match all occurrence for this regex:
log_regex.findAll(your_string_variable)
Upvotes: 0
Reputation: 25799
Short of building your regex pattern for different start_time
and end_time
, you can capture and parse the time and compare it with actual time range:
import datetime
start_time = datetime.time(9, 00)
end_time = datetime.time(9, 30)
log_line = "09:00:00.648711172 [Info ] [....]"
log_time = datetime.datetime.strptime(log_line[:log_line.find(".")], "%H:%M:%S").time()
if start_time <= log_time <= end_time:
print("Woo, we've found a match: {}".format(log_line))
else:
print("Close, but no cigar with the line: {}".format(log_line))
Upvotes: 2
Reputation: 13356
Only regex won't help you much in this case. Rather you can try to pick up the values of hour, minute and second using a regex, then do some calculation (something like hour * 3600 + minute * 60 + second
) to determine if it's in the range or not.
Upvotes: 0