hard +25 pts

Match IPv6 Address

Validate IPv6 addresses with a single regex that honors RFC 4291 rules.

Write a function `is_valid_ipv6(s: str) -> bool` that returns `True` if the string `s` is a valid fully expanded IPv6 address, and `False` otherwise. An IPv6 address is defined as exactly **8 groups** of hexadecimal digits, each group being 1 to 4 digits, separated by colons (`:`). The groups must contain only characters `0-9`, `a-f`, `A-F` (case-insensitive). No extra characters (spaces, colons beyond separators, etc.) are allowed. The address must be fully expanded — **the `::` shorthand is NOT allowed**. Use a single regular expression to perform the validation inside your function. Your solution should not rely on external libraries. **Examples:** - `'2001:0db8:85a3:0000:0000:8a2e:0370:7334'` → True - `'2001:db8:85a3:0:0:8A2E:0370:7334'` → True - `'2001:db8:85a3::8A2E:0370:7334'` → False (uses `::`) - `'12345:6789:abcd:ef01:2345:6789:abcd:ef01'` → False (a group has 5 digits) - `'2001:db8:85a3:0:0:8A2E:0370:7334:'` → False (trailing colon) - `'g001:db8:85a3:0:0:8A2E:0370:7334'` → False (invalid hex character)

Constraints

Input `s` is a string with length between 0 and 100. Expected time complexity O(N) where N is the length of the string.

Example

>>> is_valid_ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334')
True
>>> is_valid_ipv6('2001:db8:85a3:0:0:8A2E:0370:7334')
True
>>> is_valid_ipv6('2001:db8:85a3::8A2E:0370:7334')
False
>>> is_valid_ipv6('12345:6789:abcd:ef01:2345:6789:abcd:ef01')
False
>>> is_valid_ipv6('2001:db8:85a3:0:0:8A2E:0370:7334:')
False
>>> is_valid_ipv6('g001:db8:85a3:0:0:8A2E:0370:7334')
False
25 points ~30 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

An IPv6 group is one to four hex digits: `[0-9a-fA-F]{1,4}`.
You need exactly eight groups separated by colons. Use `^` and `$` anchors.
Build a regex like `([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}` — but remember to anchor it properly.
Use `re.fullmatch` to ensure the entire string is consumed.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.