Reputation:
I am new to python and trying to make a simple but understandable regex to match a file containing these lines:
Month(2 letters), day and time
Sep 15 04:34:02
Regex so far: I have managed to match the month, but dont know how to match the space and the digits. Hope someone can help me with this:
with open('file.txt','r') as file:
for line in file:
fil = re.findall('^\D{3}\d{1,2}',line)
print " ".join(fil)
Upvotes: 1
Views: 3873
Reputation: 13640
You can use the following:
^\D{3}\s+\d{1,2}\s+\d{2}(:\d{2}){2}
Explanation:
\D{3}
any three non digits\s+
one or more white spaces\d{1,2}
one to two digits\d{2}
exactly two digits(:\d{2}){2}
two occurrences of (:
followed by two digits)Tip 1: To make it more accurate replace \D
with [a-zA-Z]
because you dont want to allow any non digits here..
Tip 2: You would also want to use [0-2]\d(:[0-5]\d){2}
for matching time..
Final regex:
^[a-zA-Z]{3}\s+\d{1,2}\s+[0-2]\d(:[0-5]\d){2}
Upvotes: 3
Reputation: 1047
Try it:
[A-Za-z]{3}\s\d{1,2}\s(?:\d{1,2}:){2}\d{1,2}
[A-Za-z]{3}
-> three letters -> Sep
\s
-> space
\d{1,2}
-> two digits -> 15
\s
-> space
(?:\d{1,2}:){2}\d{1,2}
-> time -> 04:34:02
Upvotes: 1