This code works fine. Generates a dictionary of the loaded data. Image 1 corresponds to the data and image 2 corresponds to the generated dictionary.
Image 1:
Image 2:
Code:
import re
def logs():
with open("assets/logdata.txt", "r") as file:
logdata = file.read()
logs = []
w = '(?P<host>(?:\d+\.){3}\d+)\s+(?:\S+)\s+(?P<user_name>\S+)\s+\[(?P<time>[-+\w\s:/]+)\]\s+"(?P<request>.+?.+?)"'
for m in re.finditer(w, logdata):
logs.append(m.groupdict())
print (logs)
print(len(logs))
logs()
Question: I've read a lot about "re" in the Python documentation, but there are things I don't understand. Can someone explain to me step by step what this character line does?
w = '(?P<host>(?:\d+\.){3}\d+)\s+(?:\S+)\s+(?P<user_name>\S+)\s+\[(?P<time>[-+\w\s:/]+)\]\s+"(?P<request>.+?.+?)"'