SoFunction
Updated on 2025-03-09

In-depth explanation of IPV4 and IPV6 regular expressions

IPV4 regular expressions

Ipv4 addresses are divided into five categories: ABCDE, among which ABC class is a normal IP address, D class is a multicast address, and E class is reserved for research purposes.

The ranges are:

A: 1.0.0.1 - 126.155.255.255

Intranet address range: 10.0.0.0 1110-255.255.255

B: 127.0.0.1 —191.255.255.255

Intranet address range: 172.16.0.0-172.31.255.255

C: 192.0.0.1 —223.255.255.255

Intranet address range: 192.168.0.0—192.168.255.255

D: 224.0.0.1 —239.255.255.255

E: 240.0.0.1 —255.255.255.255

Our regular requirement that ip must be an ABC class address. The first number of each byte can be 0, say 01, 001.

1. The first byte of ip is divided into the following situations:

1. Length is 3 and starts with 2, range is 200-223

Regularity: 22[0-3]丨2[0-1][0-9]

2. Length is 3 and starts with 0 or 1

Regular: [0-1][0-9][0-9]

3. Length is 1 or 2

Regular: ([0-9])]{1,2}

So the expression of the first byte is:

(22[0-3]丨2[0-1][0-9]|

[0-1][0-9][0-9]|

0[1 -9][0-9]|

([0-9])]{1,2})

2.    The next three bytes range is 0-255, and there is a point in front

It is divided into the following situations:

1. Start with 2

Regular: 25[0-5]|2[0-4][0-9]

2. The situation starting with 1 and 0 is the same as the first byte.

So, a single byte regular expression:

([.]

(25[0-5]|2[0-4][0-9]|

[0-1][0-9][0-9]|

0[1 -9][0-9]|

([0-9])]{1,2}))

3. Add the dot and repeat three times, as well as the start and end matches, the final regular expression of IPV4 becomes:

((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})){3})

The reason why dots are enclosed in brackets is that if they are not expanded, they match any character. It can also be escaped with two backslash rods.

Add the line start and end match characters:

(^((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})$)

IPV6 regular expressions

Introduction to IPV6

The length of IPV6 is 128 bits, which greatly expands the available space of IP address compared to 32 bits of IPv4. The ipv4 address is now regarded as a scarce resource, while the ipv6 address is quite sufficient and will be endless for the foreseeable future. There is a description: If the earth's surface (including land and water) are covered with computers, then IPv6 allows 7*10A23 IP addresses per square meter; if the address allocation rate is 1 million per microsecond, it will take 10A19 years to allocate all addresses.

IPv6 address representation

The 128-bit address of IPv6 is usually written in 8 groups, each group in the form of four hexadecimal numbers. for example:

AD80:0000:0000:0000:ABAA:0000:00C2:0002

It is a legal IPv6 address. This address is relatively long and looks inconvenient and easy to write. The zero compression method can be used to reduce its length. If the values ​​of several consecutive ranks are 0, then these 0s can be simply represented by ::, and the above address can be written as:

AD80::ABAA:0000:00C2:0002

This simplification can only be used once, and the 0000 after ABAA in the above example cannot be simplified again. Of course, you can also use :: after ABAA, so that the 12 0s in the first cannot be compressed. The purpose of this limitation is to accurately restore the compressed 0, otherwise it will be impossible to determine how many 0s each :: represents. For example, here are some legitimate IPv6 addresses:

CDCD:910A:2222:5498:8475:1111:3900:2020

1030::C9B4:FF12:48AA:1A2B

2000:0:0:0:0:0:0:1::

0:0:0:0:0:0:12000:0:0:0:0::

At the same time, the zeros in front of each segment can be omitted, so

2001:0DB8:02de::0e13 is equivalent to

2001:DB8:2de::e13

An IPv6 address can embed a full IPv4 address and write it as a mixture of IPv6 form and the usual IPv4 form.

There are two ways to embed IPv4 in IPv6: IPv4 image address and IPv4 compatible address (has been discarded).

IPv4 image address

0000:0000:0000:0000:0000:ffff:192.168.89.9 The corresponding IPv6 address of this mixed writing method:

0000:0000:0000:0000:0000:ffff:c0a8:5909

In fact, it represents the ipv4 address 192.168.89.9. The IPv4 image address layout is as follows:

0000…..0000(80bits)| FFFF | IPv4 address |

IPv4 compatible address

The difference between a compatible address and an image address is that bits 81-96 are 0.

The IPv4 compatible address layout is as follows:

0000…..0000(80bits) | 0000 | IPv4 address |

The format is divided into the following situations:

1.     The first 7 complete fields, the 8th field is zero abbreviation or number. like:

1:2:3:4:5:6:7:8

1:2:3:4:5:6:7::

The corresponding regular expression is:

(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))

Adding a layer of brackets on the outermost part is to ensure that there is no confusion when splicing with other regular expressions.

2.     The first 6 complete fields, and the last 2 fields may be zero compression or ipv4 address embedded writing. like:

1:2:3:4:5:6::8

1:2:3:4:5:6::

1:2:3:4:5:6:192.168.1.1

The corresponding regular expression is:

(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)))))

3. The first 5 complete fields, the last 3 fields may be zero compression or ipv4 address embedded writing. like:

1:2:3:4:5::

1:2:3:4:5::6

1:2:3:4:5::8

1:2:3:4:5::192.168.1.1

The corresponding regular expression is:

(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)))))

4.     The first 4 complete fields, and the last 4 fields may be zero compression or ipv4 address embedded writing. like:

1:2:3:4::

1:2:3:4::5

1:2:3:4::8

1:2:3:4::192.168.1.1

The corresponding regular expression is:

(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)))))

The situation of the previous 3, 2, and 1 complete fields is omitted.

8. The first field is the beginning of abbreviation

::8

::192.168.1.1

The corresponding regular expression is:

(:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)))))

So the regular expression of IPV6 is:

(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))|

(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)|

(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)|

(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)|

(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)|

(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)|

(([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}{1,6}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)|

(:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)))))

In order to express the logic clearly, the line breaks are used and the newline is deleted, and the final regular expression of IPV6 is obtained:

(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))|([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}{1,6}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)))))

Add the line start and end match characters:

(^(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))|([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}{1,6}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:)$)

The above expression also has some incomplete considerations: the first 80 bits of compatible addresses and mapped addresses must be 0, so the above range is actually written wider, and the expression becomes more complicated. However, when I discovered this problem, there was no need for optimization, so I would like to optimize it first and optimize it if needed.

HTTPS

Regular:

((http|https|HTTP|HTTPS)://.{1,245})

Add the line start and end match characters:

(^((http|https|HTTP|HTTPS)://.{1,245})$)

domain name

Corresponding rules:

1.        Not the last paragraph

a. The character range is: a-zA-Z0-9 and dash-

b.    The start and end characters cannot be -

c.      Length not exceeding 63

2.

a.         The character range is a-zA-Z

b.         Length is 2-6

3.    There can't be only the last paragraph

Regular:

(([a-zA-Z0-9](([a-zA-Z0-9]|[-]){0-61}[a-zA-Z0-9])?[.])+[a-zA-Z0-9]{2,6}

Add the line start and end match characters:

(^(([a-zA-Z0-9](([a-zA-Z0-9]|[-]){0-61}[a-zA-Z0-9])?[.])+[a-zA-Z0-9]{2,6}$)

Empty string

(^$)

Attached IPv4 and IPv6 regular test cases-java:

import ;

public class IPv6Test {
 public static final String ipv4Regex = "(^((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})$)";
 public static final String ipv6Regex = "(^(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}{1}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}{1}|((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}{1,2}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}{1,3}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}{1,4}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}{1,5}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(([0-9A-Fa-f]{1,4}:){1}(:[0-9A-Fa-f]{1,4}{1,6}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))|(:(:[0-9A-Fa-f]{1,4}{1,7}|:((22[0-3]丨2[0-1][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})([.](25[0-5]|2[0-4][0-9]|[0-1][0-9][0-9]|0[1 -9][0-9]|([0-9])]{1,2})){3})|:))$)";
 public static final String httpRegex = "(^((http|https|HTTP|HTTPS)://.{1,245})$)";
 public static final String domainRegex = "(^(([a-zA-Z0-9](([a-zA-Z0-9]|[-]){0-61}[a-zA-Z0-9])?[.])+[a-zA-Z0-9]{2,6}$)";
 public static final String emptyRegex = "(^$)";

 public static final String finalRegex = ipv4Regex + "|" + ipv6Regex + "|" + httpRegex + "|" + domainRegex + "|" + emptyRegex;

 public static void main(String args[]) {
  try {

  } catch (Exception e) {
   ();
  }

 }

 // The first test case with field length 3 @Test
 public void testIpv4_1() {
  assert ("200.255.255.255".matches(finalRegex));
  assert ("223.255.255.255".matches(finalRegex));
  assert ("224.255.255.255".matches(finalRegex));

  assert ("192.0.0.1".matches(finalRegex));
  assert ("127.0.0.1".matches(finalRegex));
  assert ("100.0.0.1".matches(finalRegex));
  assert ("090.0.0.1".matches(finalRegex));
  assert ("009.0.0.1".matches(finalRegex));

 }

 // The first test case with a field length of 1 or 2 @Test
 public void testIpv4_2() {
  assert ("09.255.255.255".matches(finalRegex));
  assert ("90.255.255.255".matches(finalRegex));
  assert ("00.255.255.255".matches(finalRegex));

  assert (!"-.0.0.1".matches(finalRegex));
  assert ("0.0.0.1".matches(finalRegex));
  assert ("1.0.0.1".matches(finalRegex));
 }

 // Test the last three bytes @Test
 public void testIpv4_3() {
  assert ("200.0.255.255".matches(finalRegex));
  assert ("200.01.255.255".matches(finalRegex));
  assert ("200.10.255.255".matches(finalRegex));
  assert (!"200.256.255.255".matches(finalRegex));
  assert ("200.001.255.255".matches(finalRegex));

  assert ("200.255.0.255".matches(finalRegex));
  assert ("200.255.01.255".matches(finalRegex));
  assert ("200.255.10.255".matches(finalRegex));
  assert (!"200.255.256.255".matches(finalRegex));
  assert ("200.255.001.255".matches(finalRegex));

  assert ("200.255.255.0".matches(finalRegex));
  assert ("200.255.255.01".matches(finalRegex));
  assert ("200.255.255.10".matches(finalRegex));
  assert (!"200.255.255.256".matches(finalRegex));
  assert ("200.255.255.001".matches(finalRegex));

 }

 // Test exception @Test
 public void testIpv4_4() {
  assert (!"200".matches(finalRegex));
  assert (!"200.1".matches(finalRegex));
  assert (!"200.1".matches(finalRegex));
  assert (!"200.1.1".matches(finalRegex));
  assert (!"200.1.1.1.1".matches(finalRegex));
 }

 @Test
 public void testIpv6_1() {
  assert ("1:2:3:4:5:6:7::".matches(finalRegex));
  assert ("1:2:3:4:5:6:7:8".matches(finalRegex));

  assert ("1:2:3:4:5:6::".matches(finalRegex));
  assert ("1:2:3:4:5:6::8".matches(finalRegex));

  assert ("1:2:3:4:5::".matches(finalRegex));
  assert ("1:2:3:4:5::8".matches(finalRegex));

  assert ("1:2:3:4::".matches(finalRegex));
  assert ("1:2:3:4::8".matches(finalRegex));

  assert ("1:2:3::".matches(finalRegex));
  assert ("1:2:3::8".matches(finalRegex));

  assert ("1:2::".matches(finalRegex));
  assert ("1:2::8".matches(finalRegex));

  assert ("1::".matches(finalRegex));
  assert ("1::8".matches(finalRegex));

  assert ("::".matches(finalRegex));
  assert ("::8".matches(finalRegex));
  assert ("::7:8".matches(finalRegex));
  assert ("::6:7:8".matches(finalRegex));
  assert ("::5:6:7:8".matches(finalRegex));
  assert ("::4:5:6:7:8".matches(finalRegex));
  assert ("::3:4:5:6:7:8".matches(finalRegex));
  assert ("::2:3:4:5:6:7:8".matches(finalRegex));

  assert ("::192.168.1.1".matches(finalRegex));

 }

 @Test
 public void testIpv6_2() {
  assert ("A:0f:0F:FFFF:5:6:7:8".matches(finalRegex));
  assert (!"A:0f:0F:FFFF1:5:6:7:8".matches(finalRegex));
  assert (!"G:0f:0F:FFFF:5:6:7:8".matches(finalRegex));
 }

 @Test
 public void testHttp() {
  assert ("".matches(finalRegex));
  assert ("".matches(finalRegex));
  assert ("https://a".matches(finalRegex));
  assert ("".matches(finalRegex));
  assert (!""
    .matches(finalRegex));
 }

 @Test
 public void testDomain() {
  assert ("".matches(finalRegex));
  assert ("".matches(finalRegex));
  assert (!"a.-".matches(finalRegex));
  assert ("".matches(finalRegex));
  assert (!"".matches(finalRegex));
 }

 @Test
 public void testEmpty() {
  assert ("".matches(finalRegex));
  assert (!"1".matches(finalRegex));

 }
}

This is the end of this article about IPV4 and IPV6 regular expressions. For more related IPV4 and IPV6 regular expressions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!