I have been looking high and low for correct and efficiently designed Regular Expressions (RegEx, RegExp), for various purposes, but in this post, I’m focusing on IPv4 and IPv6 address validation.
The target environments for the regular expressions in this particular case are JavaScript (Web Browser, NodeJS) as well as HTML5 pattern attribute usage.
The regular expressions I have found online so far, have not met either the correctness and/or efficiency criteria very well, so far. I will proceed to give some examples of these issues and an incomplete set of test cases below, in order to describe the problems encountered.
Goals – Regular Expressions
- Backtracking should be minimized, where possible.
- Classes and portions of patterns should be ordered based on expected frequency, if possible.
- Any other performance and conciseness considerations should be followed, where possible.
Goals – IPv6:
- Leading zeros should be allowed if and only if the address is completely uncollapsed (completely expanded).
- Single segment zero sequences should not be collapsed.
- If a sequence of zeros is collapsed, the complete sequence of adjacent zero segments should be collapsed (just a portion of such a sequence should not be collapsed, and there should be no remaining adjacent zero segments by the collapsed sequence).
- As per specification, if two separate equal length sequences of zero segments that are the longest exist, only the first such sequence should be collapsed.
Goals – IPv6 dual-stack:
- Deprecated compatible syntax should be allowed.
- As per specification, should start with a sequence of six zero segments for compatible, and a sequence of five zero segments and an FFFF segment for mapped address notation. (Many existing solutions get this wrong.)
- Last collapsed sequence :: omission should be optional (both variants should be allowed).
Patterns already tried
(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])) from Here. While this doesn’t even pass all of the test cases, there are many other fails not included in the current set of test-cases such as multiple sequence collapsing.
(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}|::(?:[0-9A-Fa-f]{1,4}:){5}|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}|(?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}|(?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:|(?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::) from Here
- and many others…
Test Cases
The following were derived in part based on These, but are used differently in many cases.
let ipv6Valid = [
'0000:0000:0000:0000:0000:0000:0000:0000/128',
'0000:0000:0000:0000:0000:0000:0000:0000',
'0000:0000:0000:0000:0000:0000:0000:0001',
'0:0:0:0:0:0:0:0',
'0:0:0:0:0:0:0:1',
'0:0:0:0:1:0:0:0',
'0:1:2:3:4:5:6:7/128',
'0:1:2:3:4:5:6:7',
'1080:0:0:0:8:800:200c:417a',
'1080::8:800:200c:417a',
'1111:2222:3333:4444:5555:6666:7777:8888',
'1111:2222:3333:4444:5555:6666::',
'1111:2222:3333:4444:5555::',
'1111:2222:3333:4444:5555::8888',
'1111:2222:3333:4444::',
'1111:2222:3333:4444::7777:8888',
'1111:2222:3333:4444::8888',
'1111:2222:3333::',
'1111:2222:3333::6666:7777:8888',
'1111:2222:3333::7777:8888',
'1111:2222:3333::8888',
'1111:2222::',
'1111:2222::5555:6666:7777:8888',
'1111:2222::6666:7777:8888',
'1111:2222::7777:8888',
'1111:2222::8888',
'1111::',
'1111::4444:5555:6666:7777:8888',
'1111::5555:6666:7777:8888',
'1111::6666:7777:8888',
'1111::7777:8888',
'1111::8888',
'1:2:3:4:5:6:7:8',
'1:2:3:4:5:6::',
'1:2:3:4:5::',
'1:2:3:4:5::8',
'1:2:3:4::',
'1:2:3:4::7:8',
'1:2:3:4::8',
'1:2:3::',
'1:2:3::7:8',
'1:2:3::8',
'1:2::',
'1:2::7:8',
'1:2::8',
'1::',
'1::2:3',
'1::2:3:4',
'1::2:3:4:5',
'1::2:3:4:5:6',
'1::7:8',
'1::8',
'2001:0000:1234:0000:0000:C1C0:ABCD:0876',
'2001:0000:4136:e378:8000:63bf:3fff:fdd2',
'2001:0DB8:0000:CD30:0000:0000:0000:0000/60',
'2001:DB8:0:CD30::/60',
'2001:DB8:0:CD30/60',
'2001:0db8:0000:0000:0000:0000:1428:57ab',
'2001:0db8:0:0:0:0:1428:57ab',
'2001:0db8:1234:0000:0000:0000:0000:0000',
'2001:db8:1234::',
'2001:0db8:1234:ffff:ffff:ffff:ffff:ffff',
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'2001:db8::1428:57ab',
'2001:DB8:0:0:8:800:200C:417A',
'2001:DB8::8:800:200C:417A',
'2001:db8:85a3:0:0:8a2e:370:7334',
'2001:db8:85a3::8a2e:370:7334',
'2001:db8::',
'2001:db8::1428:57ab',
'2001:db8:a::123',
'2002::',
'2608::3:5',
'2608:af09:30:0:0:0:0:134',
'2::10',
'3ffe:0b00:0000:0000:0001:0000:0000:000a',
'7:6:5:4:3:2:1:0',
'::',
'::/128',
'::1',
'::1/128',
'::2:3',
'::2:3:4',
'::2:3:4:5',
'::2:3:4:5:6',
'::2:3:4:5:6:7',
'::3333:4444:5555:6666:7777:8888',
'::4444:5555:6666:7777:8888',
'::5555:6666:7777:8888',
'::6666:7777:8888',
'::7777:8888',
'::8',
'::8888',
'::ffff:0:0',
'::ffff:c000:280',
'FE80::/10',
'FEC0::/10',
'FF00::/8',
'FF01:0:0:0:0:0:0:101',
'FF01::101',
'FF02:0000:0000:0000:0000:0000:0000:0001',
'fe80:0000:0000:0000:0204:61ff:fe9d:f156',
'fe80:0:0:0:204:61ff:fe9d:f156',
'fe80::',
'fe80::1',
'fe80::204:61ff:fe9d:f156',
'fe80::217:f2ff:fe07:ed62',
'fedc:ba98:7654:3210:fedc:ba98:7654:3210',
'ff02::1',
'ffff::',
'ffff::3:5',
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
]
let ipv6Invalid = [
'1:0:0:0:1::1',
'10.0.0.1',
'-1',
'::1 ::1',
'02001:0000:1234:0000:0000:C1C0:ABCD:0876',
'1.2.3.4',
'1.2.3.4:1111:2222:3333:4444::5555',
'1.2.3.4:1111:2222:3333::5555',
'1.2.3.4:1111:2222::5555',
'1.2.3.4:1111::5555',
'1.2.3.4::',
'1.2.3.4::5555',
'1111',
'11112222:3333:4444:5555:6666:1.2.3.4',
'11112222:3333:4444:5555:6666:7777:8888',
'::1//64',
'::1/0001',
'1111:',
'1111:1.2.3.4',
'1111:2222',
'1111:22223333:4444:5555:6666:1.2.3.4',
'1111:22223333:4444:5555:6666:7777:8888',
'1111:2222:',
'1111:2222:1.2.3.4',
'1111:2222:3333',
'1111:2222:33334444:5555:6666:1.2.3.4',
'1111:2222:33334444:5555:6666:7777:8888',
'1111:2222:3333:',
'1111:2222:3333:1.2.3.4',
'1111:2222:3333:4444',
'1111:2222:3333:44445555:6666:1.2.3.4',
'1111:2222:3333:44445555:6666:7777:8888',
'1111:2222:3333:4444:',
'1111:2222:3333:4444:1.2.3.4',
'1111:2222:3333:4444:5555',
'1111:2222:3333:4444:55556666:1.2.3.4',
'1111:2222:3333:4444:55556666:7777:8888',
'1111:2222:3333:4444:5555:',
'1111:2222:3333:4444:5555:1.2.3.4',
'1111:2222:3333:4444:5555:6666',
'1111:2222:3333:4444:5555:66661.2.3.4',
'1111:2222:3333:4444:5555:66667777:8888',
'1111:2222:3333:4444:5555:6666:',
'1111:2222:3333:4444:5555:6666:00.00.00.00',
'1111:2222:3333:4444:5555:6666:000.000.000.000',
'1111:2222:3333:4444:5555:6666:1.2.3.4.5',
'1111:2222:3333:4444:5555:6666:255.255.255255',
'1111:2222:3333:4444:5555:6666:255.255255.255',
'1111:2222:3333:4444:5555:6666:255255.255.255',
'1111:2222:3333:4444:5555:6666:256.256.256.256',
'1111:2222:3333:4444:5555:6666:7777',
'1111:2222:3333:4444:5555:6666:77778888',
'1111:2222:3333:4444:5555:6666:7777:',
'1111:2222:3333:4444:5555:6666:7777:1.2.3.4',
'1111:2222:3333:4444:5555:6666:7777:8888:',
'1111:2222:3333:4444:5555:6666:7777:8888:1.2.3.4',
'1111:2222:3333:4444:5555:6666:7777:8888:9999',
'1111:2222:3333:4444:5555:6666:7777:8888::',
'1111:2222:3333:4444:5555:6666:7777:::',
'1111:2222:3333:4444:5555:6666::1.2.3.4',
'1111:2222:3333:4444:5555:6666::8888:',
'1111:2222:3333:4444:5555:6666:::',
'1111:2222:3333:4444:5555:6666:::8888',
'1111:2222:3333:4444:5555::7777:8888:',
'1111:2222:3333:4444:5555::7777::',
'1111:2222:3333:4444:5555::8888:',
'1111:2222:3333:4444:5555:::',
'1111:2222:3333:4444:5555:::1.2.3.4',
'1111:2222:3333:4444:5555:::7777:8888',
'1111:2222:3333:4444::5555:',
'1111:2222:3333:4444::6666:7777:8888:',
'1111:2222:3333:4444::6666:7777::',
'1111:2222:3333:4444::6666::8888',
'1111:2222:3333:4444::7777:8888:',
'1111:2222:3333:4444::8888:',
'1111:2222:3333:4444:::',
'1111:2222:3333:4444:::6666:1.2.3.4',
'1111:2222:3333:4444:::6666:7777:8888',
'1111:2222:3333::5555:',
'1111:2222:3333::5555:6666:7777:8888:',
'1111:2222:3333::5555:6666:7777::',
'1111:2222:3333::5555:6666::8888',
'1111:2222:3333::5555::1.2.3.4',
'1111:2222:3333::5555::7777:8888',
'1111:2222:3333::6666:7777:8888:',
'1111:2222:3333::7777:8888:',
'1111:2222:3333::8888:',
'1111:2222:3333:::',
'1111:2222:3333:::5555:6666:1.2.3.4',
'1111:2222:3333:::5555:6666:7777:8888',
'1111:2222::4444:5555:6666:7777:8888:',
'1111:2222::4444:5555:6666:7777::',
'1111:2222::4444:5555:6666::8888',
'1111:2222::4444:5555::1.2.3.4',
'1111:2222::4444:5555::7777:8888',
'1111:2222::4444::6666:1.2.3.4',
'1111:2222::4444::6666:7777:8888',
'1111:2222::5555:',
'1111:2222::5555:6666:7777:8888:',
'1111:2222::6666:7777:8888:',
'1111:2222::7777:8888:',
'1111:2222::8888:',
'1111:2222:::',
'1111:2222:::4444:5555:6666:1.2.3.4',
'1111:2222:::4444:5555:6666:7777:8888',
'1111::3333:4444:5555:6666:7777:8888:',
'1111::3333:4444:5555:6666:7777::',
'1111::3333:4444:5555:6666::8888',
'1111::3333:4444:5555::1.2.3.4',
'1111::3333:4444:5555::7777:8888',
'1111::3333:4444::6666:1.2.3.4',
'1111::3333:4444::6666:7777:8888',
'1111::3333::5555:6666:1.2.3.4',
'1111::3333::5555:6666:7777:8888',
'1111::4444:5555:6666:7777:8888:',
'1111::5555:',
'1111::5555:6666:7777:8888:',
'1111::6666:7777:8888:',
'1111::7777:8888:',
'1111::8888:',
'1111:::',
'1111:::3333:4444:5555:6666:1.2.3.4',
'1111:::3333:4444:5555:6666:7777:8888',
'123',
'12345::6:7:8',
'124.15.6.89/60',
'1:2:3:4:5:6:7:8:9',
'1:2:3::4:5:6:7:8:9',
'1:2:3::4:5::7:8',
'1::1.2.256.4',
'1::1.2.3.256',
'1::1.2.3.300',
'1::1.2.3.900',
'1::1.2.300.4',
'1::1.2.900.4',
'1::1.256.3.4',
'1::1.300.3.4',
'1::1.900.3.4',
'1::256.2.3.4',
'1::260.2.3.4',
'1::2::3',
'1::300.2.3.4',
'1::300.300.300.300',
'1::3000.30.30.30',
'1::400.2.3.4',
'1::5:1.2.256.4',
'1::5:1.2.3.256',
'1::5:1.2.3.300',
'1::5:1.2.3.900',
'1::5:1.2.300.4',
'1::5:1.2.900.4',
'1::5:1.256.3.4',
'1::5:1.300.3.4',
'1::5:1.900.3.4',
'1::5:256.2.3.4',
'1::5:260.2.3.4',
'1::5:300.2.3.4',
'1::5:300.300.300.300',
'1::5:3000.30.30.30',
'1::5:400.2.3.4',
'1::5:900.2.3.4',
'1::900.2.3.4',
'1:::3:4:5',
'2001:0000:1234: 0000:0000:C1C0:ABCD:0876',
'2001:0000:1234:0000:00001:C1C0:ABCD:0876',
'2001:0000:1234:0000:0000:C1C0:ABCD:0876 0',
'2001:1:1:1:1:1:255Z255X255Y255',
'2001::FFD3::57ab',
'2001:DB8:0:0:8:800:200C:417A:221',
'2001:db8:85a3::8a2e:37023:7334',
'2001:db8:85a3::8a2e:370k:7334',
'3ffe:0b00:0000:0001:0000:0000:000a',
'3ffe:b00::1::a',
':',
':1.2.3.4',
':1111:2222:3333:4444:5555:6666:1.2.3.4',
':1111:2222:3333:4444:5555:6666:7777:8888',
':1111:2222:3333:4444:5555:6666:7777::',
':1111:2222:3333:4444:5555:6666::',
':1111:2222:3333:4444:5555:6666::8888',
':1111:2222:3333:4444:5555::',
':1111:2222:3333:4444:5555::1.2.3.4',
':1111:2222:3333:4444:5555::7777:8888',
':1111:2222:3333:4444:5555::8888',
':1111:2222:3333:4444::',
':1111:2222:3333:4444::1.2.3.4',
':1111:2222:3333:4444::5555',
':1111:2222:3333:4444::6666:1.2.3.4',
':1111:2222:3333:4444::6666:7777:8888',
':1111:2222:3333:4444::7777:8888',
':1111:2222:3333:4444::8888',
':1111:2222:3333::',
':1111:2222:3333::1.2.3.4',
':1111:2222:3333::5555',
':1111:2222:3333::5555:6666:1.2.3.4',
':1111:2222:3333::5555:6666:7777:8888',
':1111:2222:3333::6666:1.2.3.4',
':1111:2222:3333::6666:7777:8888',
':1111:2222:3333::7777:8888',
':1111:2222:3333::8888',
':1111:2222::',
':1111:2222::1.2.3.4',
':1111:2222::4444:5555:6666:1.2.3.4',
':1111:2222::4444:5555:6666:7777:8888',
':1111:2222::5555',
':1111:2222::5555:6666:1.2.3.4',
':1111:2222::5555:6666:7777:8888',
':1111:2222::6666:1.2.3.4',
':1111:2222::6666:7777:8888',
':1111:2222::7777:8888',
':1111:2222::8888',
':1111::',
':1111::1.2.3.4',
':1111::3333:4444:5555:6666:1.2.3.4',
':1111::3333:4444:5555:6666:7777:8888',
':1111::4444:5555:6666:1.2.3.4',
':1111::4444:5555:6666:7777:8888',
':1111::5555',
':1111::5555:6666:1.2.3.4',
':1111::5555:6666:7777:8888',
':1111::6666:1.2.3.4',
':1111::6666:7777:8888',
':1111::7777:8888',
':1111::8888',
':2222:3333:4444:5555:6666:1.2.3.4',
':2222:3333:4444:5555:6666:7777:8888',
':3333:4444:5555:6666:1.2.3.4',
':3333:4444:5555:6666:7777:8888',
':4444:5555:6666:1.2.3.4',
':4444:5555:6666:7777:8888',
':5555:6666:1.2.3.4',
':5555:6666:7777:8888',
':6666:1.2.3.4',
':6666:7777:8888',
':7777:8888',
':8888',
'::-1',
'::.',
'::..',
'::...',
'::...4',
'::..3.',
'::..3.4',
'::.2..',
'::.2.3.',
'::.2.3.4',
'::1...',
'::1.2..',
'::1.2.256.4',
'::1.2.3.',
'::1.2.3.256',
'::1.2.3.300',
'::1.2.3.900',
'::1.2.300.4',
'::1.2.900.4',
'::1.256.3.4',
'::1.300.3.4',
'::1.900.3.4',
'::1111:2222:3333:4444:5555:6666::',
'::2222:3333:4444:5555:6666:7777:1.2.3.4',
'::2222:3333:4444:5555:6666:7777:8888:',
'::2222:3333:4444:5555:6666:7777:8888:9999',
'::2222:3333:4444:5555:7777:8888::',
'::2222:3333:4444:5555:7777::8888',
'::2222:3333:4444:5555::1.2.3.4',
'::2222:3333:4444:5555::7777:8888',
'::2222:3333:4444::6666:1.2.3.4',
'::2222:3333:4444::6666:7777:8888',
'::2222:3333::5555:6666:1.2.3.4',
'::2222:3333::5555:6666:7777:8888',
'::2222::4444:5555:6666:1.2.3.4',
'::2222::4444:5555:6666:7777:8888',
'::256.2.3.4',
'::260.2.3.4',
'::300.2.3.4',
'::300.300.300.300',
'::3000.30.30.30',
'::3333:4444:5555:6666:7777:8888:',
'::400.2.3.4',
'::4444:5555:6666:7777:8888:',
'::5555:',
'::5555:6666:7777:8888:',
'::6666:7777:8888:',
'::7777:8888:',
'::8888:',
'::900.2.3.4',
':::',
':::1.2.3.4',
':::2222:3333:4444:5555:6666:1.2.3.4',
':::2222:3333:4444:5555:6666:7777:8888',
':::3333:4444:5555:6666:7777:8888',
':::4444:5555:6666:1.2.3.4',
':::4444:5555:6666:7777:8888',
':::5555',
':::5555:6666:1.2.3.4',
':::5555:6666:7777:8888',
':::6666:1.2.3.4',
':::6666:7777:8888',
':::7777:8888',
':::8888',
'::ffff:192x168.1.26',
'::ffff:2.3.4',
'::ffff:257.1.2.3',
'FF01::101::2',
'FF02:0000:0000:0000:0000:0000:0000:0000:0001',
'XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:1.2.3.4',
'XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX',
'a::b::c',
'a::g',
'a:a:a:a:a:a:a:a:a',
'a:aaaaa::',
'a:b',
'a:b:c:d:e:f:g:0',
'fe80:0000:0000:0000:0204:61ff:254.157.241.086',
'ffff:',
'ffff::ffff::ffff',
'ffgg:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
'ldkfj',
'::/129',
'1000:://32',
'::/',
'0:0:0:0:0:0:0::',
'0:0:0:0:0:0::',
'0:0:0:0:0::',
'0:0:0:0::',
'0:0:0::',
'0:0::',
'0::',
'0:a:b:c:d:e:f::',
'1111:2222:3333:4444:5555:6666:123.123.123.123',
'1111:2222:3333:4444:5555:6666:7777::',
'1111:2222:3333:4444:5555:6666::8888',
'1111:2222:3333:4444:5555::123.123.123.123',
'1111:2222:3333:4444:5555::7777:8888',
'1111:2222:3333:4444::6666:7777:8888',
'1111:2222:3333::5555:6666:7777:8888',
'1111:2222::4444:5555:6666:7777:8888',
'1111::3333:4444:5555:6666:7777:8888',
'::2222:3333:4444:5555:6666:7777:8888',
'1:2:3:4:5:6::8',
'1:2:3:4:5::7:8',
'1::2:3:4:5:6:7',
'2001:0db8:0000:0000:0000::1428:57ab',
'2001::CE49:7601:2CAD:DFFF:7C94:FFFE',
'2001::CE49:7601:E866:EFFF:62C3:FFFE',
'2608:af09:30::102a:7b91:c239:baff',
'::0',
'::0:0',
'::0:0:0',
'::0:0:0:0',
'::0:0:0:0:0',
'::0:0:0:0:0:0',
'::0:0:0:0:0:0:0',
'::0:a:b:c:d:e:f',
'a:b:c:d:e:f:0::',
'a:0::0:b',
'a:0:0::0:b',
'a:0::0:0:b',
'a::0:0:b',
'a::0:b',
'a:0::b',
'a:0:0::b',
'::ffff:0c22:384e',
'::2:3:4:5:6:7:8',
'2001:0DB8::CD30:0:0:0:0/60',
'2001:0db8:0:0::1428:57ab',
'0:1:2:3:4:5:6:7/001',
]
Test Cases – Dual-stack
let ipv6HybridValid = [
'0:0:0:0:0:0:13.1.68.3',
'0:0:0:0:0:FFFF:129.144.52.38',
'::FFFF:129.144.52.38',
'::ffff:12.34.56.78',
'::ffff:192.0.2.128',
'::ffff:192.168.1.1',
'::ffff:192.168.1.26',
'::123.123.123.123',
'::13.1.68.3',
]
let ipv6HybridInvalid = [
'1111:2222:3333:4444:5555::123.123.123.123',
'1111:2222:3333:4444::123.123.123.123',
'1111:2222:3333:4444::6666:123.123.123.123',
'1111:2222:3333::123.123.123.123',
'1111:2222:3333::5555:6666:123.123.123.123',
'1111:2222:3333::6666:123.123.123.123',
'1111:2222::123.123.123.123',
'1111:2222::4444:5555:6666:123.123.123.123',
'1111:2222::5555:6666:123.123.123.123',
'1111:2222::6666:123.123.123.123',
'1111::123.123.123.123',
'1111::3333:4444:5555:6666:123.123.123.123',
'1111::4444:5555:6666:123.123.123.123',
'1111::5555:6666:123.123.123.123',
'1111::6666:123.123.123.123',
'1:2:3:4:5:6:1.2.3.4',
'1:2:3:4:5::1.2.3.4',
'1:2:3:4::1.2.3.4',
'1:2:3:4::5:1.2.3.4',
'1:2:3::1.2.3.4',
'1:2:3::5:1.2.3.4',
'1:2::1.2.3.4',
'1:2::5:1.2.3.4',
'1::1.2.3.4',
'1::5:1.2.3.4',
'1::5:11.22.33.44',
'::4444:5555:6666:123.123.123.123',
'::5555:6666:123.123.123.123',
'::6666:123.123.123.123',
'fe80:0:0:0:204:61ff:254.157.241.86',
'fe80::204:61ff:254.157.241.86',
'fe80::217:f2ff:254.7.237.98',
]
I may have missed mentioning a few points/goals above, but if I notice such, I will try to edit and update.
If anything is unclear about the requirements, please inquire in comments.
I already have written a complex regular expression in an attempt to meet these goals, but esp. due to the incompleteness of test-cases, I’m still unsure if the goals are achieved. I will try to post my solution as an answer below, for anyone who encountered the same issues as I have.
This question was intended to be a Q&A pair (self-answering), but I’m also asking the community about the correctness of the test-cases and my own solutions.
If any bugs/errata/issues are discovered after posting, I will try to update my question and answer accordingly, attempting to address those.