find ipv6 addresses in a string using regex in python

Here is an example code to extract ipv6 addresses from a string using Python's regular expression module re:

main.py
import re

str_with_ipv6 = "This string contains an ipv6 address: 2001:0db8:85a3:0000:0000:8a2e:0370:7334"

ipv6_regex = r'\b(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}\b' # Regex pattern to match ipv6 address

ipv6_list = re.findall(ipv6_regex, str_with_ipv6) # find all ipv6 addresses in the string

print(ipv6_list) # ['2001:0db8:85a3:0000:0000:8a2e:0370:7334']
362 chars
10 lines

The above code will search for all patterns that match the given IPv6 regular expression in the input string. The regular expression \b(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}\b matches the IPv6 address pattern.

gistlibby LogSnag