package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?i)(?:[\da-f]{0,4}:){2,7}(?:(?<ipv4>(?:(?:25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|1?\d\d?))|[\da-f]{0,4}|:)`)
var str = `2001:0DB8:0000:CD30:0000:0000:0000:0000
2001:0DB8::CD30:0:0:0:0
2001:0DB8:0:CD30::
FF02:0:0:0:0:1:FF00:0000
FF02:0:0:0:0:1:FFFF:FFFF
2001:db8::1234:5678
FF02:0:0:0:0:0:0:2
fdf8:f53b:82e4::53
fe80::200:5aee:feaa:20a2
2001:0000:4136:e378:
8000:63bf:3fff:fdd2
2001:db8::
::1234:5678
2000::
2001:db8:a0b:12f0::1
2001:4:112:cd:65a:753:0:a1
2001:0002:6c::430
2001:5::
fe08::7:8
2001:10:240:ab::a
2002:cb0a:3cdd:1::1
2001:db8:8:4::2
FF01:0:0:0:0:0:0:2
::FFFF:0:0
2001:0000::
59fb:0:0:0:0:1005:cc57:6571 expansion of 59FB::1005:CC57:6571
# IPv4-Mapped and IPv4-Embedded
::ffff:192.0.2.47
::ffff:0.0.0.0
::ffff:255.255.255.255
::ffff:10.0.0.3
::192.168.0.1
::255.255.255.255
2001:db8:122:344::192.0.2.33
0:0:0:0:0:0:13.1.68.3
0:0:0:0:0:FFFF:129.144.52.3
::13.1.68.3
::FFFF:129.144.52.38
1:2:3:4:5:6:7:8
1::3:4:5:6:7:8
1::4:5:6:7:8
1:2:3::5:6:7:8
1::7:8
1:2:3:4:5::7:8
1:2:3:4:5::8
# Cases based on RIEP: https://www.ripe.net/participate/member-support/lir-basics/ipv6_reference_card.pdf
:0: All zeroes in a single block can be represented
::1 Loopback - This address is used when a host talks to itself over IPv6. This often happens when one program sends data to another; the IPv4 equivalent is 127.0.0.1
:: Unspecified - This address may only be used as a source address by an initializing host before it has learned its address; the IPv4 equivalent is 0.0.0.0
::ffff (i.e., ::ffff:192.0.2.47) These addresses are used to embed IPv4 addresses in an IPv6 address. One use for this is in a dual-stack transition scenario where IPv4 addresses can be mapped into an IPv6 address. See RFC 4038 for more details.
fc00:: (i.e., fdf8:f53b:82e4::53) Unique Local Addresses (ULAs) - These addresses are reserved for local use in the home and enterprise environments and are not public address spaces; the IPv4 equivalent is 10.0.0.0, 172.16.0.0, 192.168.0.0
fe80:: (i.e., fe80::200:5aee:feaa:20a2) Link-Local Addresses - These addresses are used on a single link or a non-routed common access network, such as an Ethernet LAN. They do not need to be unique outside of that link; the IPv4 equivalent is 169.254.0.0
2001:0000:: (i.e., 2001:0000:4136:e378:8000:63bf:3fff:fdd2) Teredo - This is a mapped address allowing IPv6 tunneling through IPv4 NATs. The address is formed using the Teredo prefix, the server’s unique IPv4 address, flags describing the type of NAT, the obfuscated client port, and the client IPv4 address, which is probably a private address. It is possible to reverse the process and identify the IPv4 address of the relay server, which can then be looked up in the relevant RIR’s Whois database.
2001:0002:: (i.e., 2001:0002:6c::430) Benchmarking - These addresses are reserved for use in documentation. They should not be used as source or destination addresses; the IPv4 equivalent is 198.18.0.0
# https://www.rfc-editor.org/rfc/rfc4291
2001:DB8:0:0:8:800:200C:417A a unicast address
FF01:0:0:0:0:0:0:101 a multicast address
0:0:0:0:0:0:0:1 the loopback address
0:0:0:0:0:0:0:0 the unspecified address
2001:DB8::8:800:200C:417A a unicast address
FF01::101 a multicast address
::1 the loopback address
:: the unspecified address
# https://www.ibm.com/docs/en/ts4500-tape-library?topic=functionality-ipv4-ipv6-address-formats
2001:db8:3333:4444:5555:6666:7777:8888
2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF
:: (implies all 8 segments are zero)
2001:db8:: (implies that the last six segments are zero)
::1234:5678 (implies that the first six segments are zero)
2001:db8::1234:5678 (implies that the middle four segments are zero)
2001:0db8:0001:0000:0000:0ab9:C0A8:0102 (This can be compressed to eliminate leading zeros, as follows: 2001:db8:1::ab9:C0A8:102)
2001:db8:3333:4444:5555:6666:1.2.3.4
::11.22.33.44 (implies all six IPv6 segments are zero)
2001:db8::123.123.123.123 (implies that the last four IPv6 segments are zero)
::1234:5678:91.123.4.56 (implies that the first four IPv6 segments are zero)
::1234:5678:1.2.3.4 (implies that the first four IPv6 segments are zero)
2001:db8::1234:5678:5.6.7.8 (implies that the middle two IPv6 segments are zero)
# Invalid IPv6
56FE::2159:5BBC::6594
2001:db8:a0b:12f0::::0:1
fe80:2030:31:24
::8
:::
:1
:
::2:3:4:5:6:7:8
`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/