I cheated and used combination of multiple answers submitted by other people. I think this is pretty clear and straight forward piece of code. ip_validation
should return True
or False
. Also this answer only works for IPv4 addresses
import re
ip_match = re.match('^' + '[\.]'.join(['(\d{1,3})']*4) + '$', ip_input)
ip_validate = bool(ip_match)
if ip_validate:
ip_validate &= all(map(lambda n: 0 <= int(n) <= 255, ip_match.groups())