Editor

Parse a Log Line With Regex

Intermediate · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Server logs look like 2026-10-02 14:03:55 [ERROR] Disk almost full. Write parse_log(line) that returns a dict with "date", "time", "level" and "message", or None if the line does not match. Levels are upper-case words.

{"date": "2026-10-02", "time": "14:03:55", "level": "ERROR", "message": "Disk almost full"}
import re
def parse_log(line):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
parse_log("2026-10-02 14:03:55 [ERROR] Disk almost full"){'date': '2026-10-02', 'time': '14:03:55', 'level': 'ERROR', 'message': 'Disk almost full'}
print(parse_log("not a log line"))None

More practice problems

All Python exercises · 50 practice problems