There's a ipaddress library that would validated correct for both IPv4/IPv6.
So i would think of just to use regex to get IPv4/IPv6 address whole of out text,then use ipaddress for validation.
Example.
So i would think of just to use regex to get IPv4/IPv6 address whole of out text,then use ipaddress for validation.
Example.
>>> import re >>> import ipaddress >>> >>> s = 'foo8.8.4.4bar' >>> ip = re.search(r'(\d.*\d)', s).group(1) >>> ip '8.8.4.4' >>> ipaddress.ip_address(ip) IPv4Address('8.8.4.4') # Not valid >>> s = 'foo8.256.4.4bar' >>> ip = re.search(r'(\d.*\d)', s).group(1) >>> ipaddress.ip_address(ip) Traceback (most recent call last): File "<string>", line 449, in runcode File "<interactive input>", line 1, in <module> File "C:\python37\lib\ipaddress.py", line 54, in ip_address address) ValueError: '8.256.4.4' does not appear to be an IPv4 or IPv6 address