easy +8 pts

Named group extraction

Use regex named groups to pull key fields from log lines.

Write a function `extract_info(log_line: str) -> dict or None` that parses a log line of the form: `[LEVEL] timestamp: message` - `LEVEL` is one of `INFO`, `WARN`, `ERROR`, `DEBUG` (uppercase only). - `timestamp` is a 10-digit Unix timestamp (digits only). - `message` is any non-empty sequence of characters (including spaces, but no newlines). The function must use a single regex with named groups (`(?P<level>...)`, etc.) and return a dictionary with keys `level`, `timestamp`, `message`. If the line does not match the pattern exactly, return `None`. Examples: ``` >>> extract_info("[INFO] 1620000000: Server started") {'level': 'INFO', 'timestamp': '1620000000', 'message': 'Server started'} >>> extract_info("[DEBUG] 1620000001: value=42") {'level': 'DEBUG', 'timestamp': '1620000001', 'message': 'value=42'} >>> extract_info("not a log") None ```

Constraints

- `log_line` is a string of length 1 to 500. - The message may contain any characters except newline. - The timestamp is exactly 10 digits. - The level is exactly one of the four uppercase words. - Complexity: O(n) time, O(1) extra space.

Example

>>> extract_info("[INFO] 1620000000: Server started")
{'level': 'INFO', 'timestamp': '1620000000', 'message': 'Server started'}
>>> extract_info("[DEBUG] 1620000001: value=42")
{'level': 'DEBUG', 'timestamp': '1620000001', 'message': 'value=42'}
>>> extract_info("[WARN] 1700000000: Low disk space")
{'level': 'WARN', 'timestamp': '1700000000', 'message': 'Low disk space'}
>>> extract_info("[ERROR] 1800000000: Unauthorized access")
{'level': 'ERROR', 'timestamp': '1800000000', 'message': 'Unauthorized access'}
>>> extract_info("not a log")
None
>>> extract_info("[INFO] 12345: missing timestamp digits")
None
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use raw string patterns and named groups like `(?P<level>INFO|WARN|ERROR|DEBUG)`.
Anchor the pattern with `^` and `$` to ensure the whole line matches.
Remember that `re.match` returns a match object; access groups via `m.group('level')` etc.
Use `if m:` to return a dict, else `None`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.