Parse Log Lines with Groups

Medium ⏱ 12 min 52% acceptance ★★★★★ 4.9
Log lines look like '[ERROR] 2026-07-28 payment failed'. Write parse_log(line) using one regex with named groups level, date, message, returning them as a dict, or None when the line doesn't match.

Examples

Example 1
Input
parse_log('[WARN] 2026-01-05 disk low')
Output
{'level': 'WARN', 'date': '2026-01-05', 'message': 'disk low'}
Explanation

Three named groups captured the pieces.

Constraints

  • Named groups (?P<name>...).
  • None for non-matching lines.

Topics

Regular Expressionsgroups

Companies

DatadogSplunkElastic

Hints

Hint 1

r'\[(?P<level>\w+)\] (?P<date>\d{4}-\d{2}-\d{2}) (?P<message>.+)'.

Hint 2

m.groupdict() converts to a dict.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.