98. validate an IP address

Is this helpful ?

from all members: - accepted / - tried


An IPv4 Address is represented in dot-decimal notation, consisting of 4 numbers( called 'octet'), each ranging from 0 to 255, like It has 32 bit, so there are maximum 2^32 = 4,294,967,296 IPv4 addresses.

Leading zeroes are not allowed in IPv4, like is invalid.


An IPv6 Address have 128 bits (which is a lot), separated by 8 groups (called 'hexadectet', or 'hextet', not fixed yet). Each group has 16 bits, so could be represented by a hexadecimal string at 4 digits, such as 2001:0db8:85a3:0000:0000:8a2e:0370:7334.

Notice that leading zeroes are allowed in IPv6.

There are other valid format of IPv6 addresses, like Zero compression, but it is beyond the scope here, you can ignore them.


You are given some random string, please write a function if it is valid IPv4 or IPv6 address.


Can you solve it with regular expressions ?

Let's try to solve this problem within 15 mintues.