easy +8 pts

Match IPv4 Address

Use regex to validate dotted-decimal IPv4 addresses.

Write a function `match_ipv4(address: str) -> bool` that returns `True` if the given string is a valid IPv4 address in dotted-decimal notation, and `False` otherwise. A valid IPv4 address consists of four decimal numbers separated by single dots. Each number must be an integer from 0 to 255 inclusive, with no leading zeros (except the number 0 itself). Leading or trailing whitespace should not be allowed. The entire string must exactly match the pattern — no extra characters. For example: - `"192.168.1.1"` is valid. - `"255.255.255.255"` is valid. - `"0.0.0.0"` is valid. - `"01.2.3.4"` is invalid because of the leading zero. - `"256.1.1.1"` is invalid because 256 is out of range. - `"1.2.3"` is invalid because it has only three parts. - `"1.2.3.4.5"` is invalid because it has five parts. - `"1.2.3.4 "` is invalid because of trailing whitespace. Implement the function using a single regular expression. You may use the `re` module.

Constraints

Input is a string of length between 0 and 50 characters. The function should return a boolean. The regex should be compiled with `re.fullmatch` or equivalently anchored with `^` and `$`.

Example

>>> match_ipv4("192.168.1.1")
True
>>> match_ipv4("256.1.1.1")
False
>>> match_ipv4("01.2.3.4")
False
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about how to represent numbers 0-255 without leading zeros using a regex pattern.
Remember to anchor the pattern so it matches the entire string, not just a substring.
You can use `re.fullmatch` to require the whole string to match.
Consider separating the four octets with a literal dot in the pattern.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.