Python (re)

Apache Common Log Format in PY

Parses Apache Common Log Format entries.

Try it in the PY tester →

Pattern

regexPY
^(\S+) (\S+) (\S+) \[([^\]]+)\] "(\S+) (\S+) (\S+)" (\d{3}) (\d+|-)

Python (re) code

pyPython
import re

pattern = re.compile(r"^(\\S+) (\\S+) (\\S+) \\[([^\\]]+)\\] \"(\\S+) (\\S+) (\\S+)\" (\\d{3}) (\\d+|-)")
input_text = "127.0.0.1 - frank [10/Oct/2023:13:55:36 -0700] \"GET /index.html HTTP/1.0\" 200 2326"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

Captures 9 fields: host, ident, user, timestamp, method, path, protocol, status code, and response size.

Examples

Input

127.0.0.1 - frank [10/Oct/2023:13:55:36 -0700] "GET /index.html HTTP/1.0" 200 2326

Matches

  • 127.0.0.1 - frank [10/Oct/2023:13:55:36 -0700] "GET /index.html HTTP/1.0" 200 2326

Same pattern, other engines

← Back to Apache Common Log Format overview (all engines)