Regular Expressions 101

Community Patterns

IPv6 Reserved Blocks

0

Regular Expression
PCRE (PHP <7.3)

/
(?<ipv6_full>(?:^(?:f[c-f][0-9a-f]{2}|200[1-2]|0?100):).+|(?:.+:0{0,3}1$|(?:.+:ffff:(?:0:)?|^0{0,2}64:ff9b::)(?<ipv4_map>(?:25[0-5]|2[0-4]\d|[0-1]?\d{1,2})(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d{1,2})){3})$))
/
gm

Description

Detects whether a given IPv6 string is within a reserved block, as defined on https://en.wikipedia.org/wiki/IPv6_address#Special_addresses

Please note that this assume that the string given is already a valid IPv6 address - If IPv6 validation is important to you, please make sure you pass any string through a more general IPv6 validator before testing it against this regex.

Also allows for extracting mapped IPv4 addresses from the IPv4 mapped address ranges.

Submitted by Rohaq - 6 years ago