Log Parsing with Regex

Using regular expressions to parse log entries and extract structured information

Python
import re

log_entry = '192.168.1.100 - - [20/Feb/2024:10:23:15 +0000] "GET /index.html HTTP/1.1" 200 2326'
pattern = r'(\d+\.\d+\.\d+\.\d+) .+ \[(.+)\] "(\w+) (.+) HTTP/\d\.\d" (\d+) (\d+)'
match = re.search(pattern, log_entry)
if match:
    ip, timestamp, method, path, status, bytes_sent = match.groups()
    print(f"IP: {ip}")
    print(f"Timestamp: {timestamp}")
    print(f"Method: {method}")
    print(f"Path: {path}")
    print(f"Status: {status}")
    print(f"Bytes Sent: {bytes_sent}")
CTRL + ENTER to send