0

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:

Data

Image 2:

Dictionary

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>.+?.+?)"'

Barmar
  • 669,327
  • 51
  • 454
  • 560
cfsl
  • 15
  • 6
  • Don't put "solved" in the title. If you get an answer, you mark it solved by accepting the answer. If it's closed, there's no need to change the title. – Barmar Aug 21 '21 at 22:52

0 Answers0