In depth explanation of IPv4 and IPv6 regular expressions

Time:2021-2-23

IPv4 regular expression

IPv4 addresses can be divided into five categories: ABCDE, ABC is common IP address, D is multicast address, and E is reserved for research.

The ranges are as follows

A: 1.0.0.1-126.155.255.255

Intranet address range: 10.0.0.0-10-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 request IP must be ABC class address. The first digit of each byte can be 0, such as 01, 001.

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

1. The length is 3 and starts with 2, ranging from 200 to 223

Regular: 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 last three bytes range from 0 to 255, with a dot in front

It can be divided into the following situations:

1. Start with 2

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

2. 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 match, 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-9] | 0 [1 – 9] [0-9] | ([0-9])] {1,2})) {3})

The reason why the dot is enclosed in brackets is that if it is not expanded, it will match any character. It can also be escaped with two anti skew bars.

Add the first and last line matchers:

(^ ((22 [0-3] 2 [0-1] [0-9] | [0-1] [0-9] [0-9] | [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}) $)

IPv6 regular expression

Introduction to IPv6

The length of IPv6 is 128 bits, which greatly expands the available space of IP address compared with 32 bits of IPv4. Nowadays, the IPv4 address is regarded as a scarce resource, while the IPv6 address is quite sufficient and will not be used up in the foreseeable future. There is a description: if the earth’s surface (including land and water) is covered with computers, 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 complete all address allocation.

IPv6 address representation

The 128 bit address of IPv6 is usually written in eight groups, each of which is in the form of four hexadecimal numbers. For example:

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

Is a valid IPv6 address. This address is quite long. It seems inconvenient and not easy to write. The zero compression method can be used to reduce its length. If the values of several consecutive segments are all 0, then these zeros can be simply expressed as::, and the above address can be written as:

AD80::ABAA:0000:00C2:0002

This simplification can only be used once. In the above example, 0000 after ABAA cannot be simplified again. Of course, it can also be used after ABAA, so the first 12 zeros can’t be compressed. The purpose of this restriction is to accurately restore the compressed zeros, otherwise it is impossible to determine how many zeros each:: represents. For example, here are some legal 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 zero before 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 into it and write it as a mixture of IPv6 form and usual IPv4 form.

There are two ways to embed IPv4 in IPv6: IPv4 image address and IPv4 compatible address (which have been abandoned).

IPv4 image address

0000:0000:0000:0000:0000: ffff:192.168.89.9 The IPv6 address corresponding to this hybrid writing method is as follows:

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

Actually, it represents the IPv4 address 192.168.89.9. The layout of IPv4 image address is as follows:

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

IPv4 compatible address

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

The layout of IPv4 compatible addresses is as follows:

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

The format is divided into the following situations:

1. The first seven complete fields, the eighth field is zero compression or number. For example:

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}|:))

A layer of parentheses is added on the outside to ensure no confusion when splicing with other regular expressions.

2. For the first six complete fields, the last two fields may be written in zero compression or embedded IPv4 address. For example:

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:

The [0-9a-fa-fa-f-9a-9a-fa-f] {[1,4} {[1,4} {[1,4} {[0-9a-fa-f] {[1,4 {[1,4} {[6} {[6 {[0-9a-9a-9a-fa-f] {[0-9a-9a-fa-f] {[1,4 {[1,4} {[1,4} {[1,4} {[1 {[1,4 {[1,4} {[1,4} {[1,4} {[1,4 {[0-9a-9a-9a-9a-9a-9a-9a-9a-9a-9a-9a-9a-fa-fa-fa-fa-fa-fa-fa-fa-fa-3}) |:)

3. For the first five complete fields, the last three fields may be written in zero compression or embedded IPv4 address. For example:

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-9a-9a-fa-fa-f] {[1,4] {[1,4] {[1,4] {[0-9a-9a-fa-f] {[1,4] {[1,4] {[1,4] {[1,4] {[1,4] {[5} [0-9a-9a-fa-fa-f] {[1,4] {[0-9a-9a-9a-9a-fa-fa-fa-f] {[1,4 {[1,4] {[1,4] {[1,4 {[1,4] {[1,4] {[1,4 [1,4 [1,4] {[1,4 [1,4 [1,4 [1,4] {[1,4 [1,4 [1,4] {[1,4 [1,4 [1,4 [1,4]} [1,4 [1,) {3}) |:)

4. For the first four complete fields, the last four fields may be written in zero compression or embedded IPv4 address. For example:

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-9a-9a-fa-fa-f] {[1,4] {[1,4} {[1,4] {[0-9a-fa-f] {[0-9a-9a-9a-fa-f] {[1,4 {[1,4] {[1,4] {[4 {[0-9a-9a-fa-f] {[1,4] {[0-9a-9a-9a-9a-fa-fa-fa-f] {[1,4 {[1,4] {[1,4 {[1,4} [1,4} [1,4} [1,4 {[1,4 {[1,4} [1,4 {[1,4} [1,4 {[1,4 {[1,4} [1,4} [1,4} [1,4} [1,) {3}) |:)

The previous 3, 2 and 1 complete fields are slightly different.

8. The first field is abbreviated

::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-9] | [1 – 9] [0-9] | ([0-9])]] {1,2}) ([.] (25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-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}|:))|

The [0-9a-fa-fa-f-9a-9a-fa-f] {[1,4} {[1,4} {[1,4} {[0-9a-fa-f] {[1,4 {[1,4} {[6} {[6 {[0-9a-9a-9a-fa-f] {[0-9a-9a-fa-f] {[1,4 {[1,4} {[1,4} {[1,4} {[1 {[1,4 {[1,4} {[1,4} {[1,4} {[1,4 {[0-9a-9a-9a-9a-9a-9a-9a-9a-9a-9a-9a-9a-fa-fa-fa-fa-fa-fa-fa-fa-fa-3}) |:)|

([0-9a-fa-9a-9a-fa-fa-f] {[1,4] {[1,4] {[1,4] {[0-9a-9a-fa-f] {[1,4] {[1,4] {[1,4] {[1,4] {[1,4] {[5} [0-9a-9a-fa-fa-f] {[1,4] {[0-9a-9a-9a-9a-fa-fa-fa-f] {[1,4 {[1,4] {[1,4] {[1,4 {[1,4] {[1,4] {[1,4 [1,4 [1,4] {[1,4 [1,4 [1,4 [1,4] {[1,4 [1,4 [1,4] {[1,4 [1,4 [1,4 [1,4]} [1,4 [1,) {3}) |:)|

([0-9a-fa-9a-9a-fa-fa-f] {[1,4] {[1,4} {[1,4] {[0-9a-fa-f] {[0-9a-9a-9a-fa-f] {[1,4 {[1,4] {[1,4] {[4 {[0-9a-9a-fa-f] {[1,4] {[0-9a-9a-9a-9a-fa-fa-fa-f] {[1,4 {[1,4] {[1,4 {[1,4} [1,4} [1,4} [1,4 {[1,4 {[1,4} [1,4 {[1,4} [1,4 {[1,4 {[1,4} [1,4} [1,4} [1,4} [1,) {3}) |:)|

([0-9a-fa-fa-f-9a-9a-9a-fa-f] {[1,4] {[1,4] {[0-9a-9a-fa-f] {[1,4] {[1,4] {[1,4] {[1,4] {[1,4] {[3 {[0-9a-9a-fa-fa-f] {[1,4] {[3 {[0-9a-9a-9a-9a-fa-fa-f] {[0-9a-9a-9a-9a-9a-fa-fa-fa-fa-f] {[0-9a-9a-fa-9a-9a-fa-9a-fa-fa-fa-fa-fa-fa-fa-f] {[9-fa-fa-fa-fa-fa-fa-fa-fa-f] {[1,4-fa-f] {[1,4-9) {3}) |:)|

[0-9a-fa-9a-fa-fa-9a-9a-fa-f] {[1,4] {[1,4] {[1,4] {[2 [0-9a-9a-fa-fa-f] {[1,4] {[1,4] {[2 {[0-9a-9a-9a-fa-f] {[1,4] {[2 {[0-9a-9a-9a-fa-fa-fa-f] {[1,4] {[1,4 {[1,4] {[1,4] {[1,4 {[1,4} [1,4} [1,4 [1,4 [1,4 [0-4] [0-4] [0-4] [0-4] [0-4] [0-4] [0-4] [0-4] [0-9] [0-4] [0-9] [0-9] [0-9] [0-9] [0-9] [) {3}) |:)|

([0-9a-fa-fa-fa-9a-9a-9a-fa-f] {[1,4] {[1,4] {[1,4] {[0-9a-9a-fa-f] {[1,4] {[1,4] {[1,4] {[1,4] {[1 [0-9a-9a-fa-fa-f] {[1,4] {[1 [0-9a-9a-9a-fa-fa-fa-f] {[1,4] {[1 [1,4] {[1,4] {[1,4] {[1,4] {[1,4] {[1,4 [1,4 [1,4 [1,4] {[1,4] {[1,4 [1,4] {[1,4] {[1,4] {[1,4 [0-9a-9a-9a-9a-9a-9a-9a) {3}) |:)|

(: (: [0-9a-fa-f] {1,4} {1,7} |: ((22 [0-3] 2 [0-1] [0-9] | [0-1] [0-9] [0-9] | [1 – 9] [0-9] | ([0-9])]] {1,2}) ([.] (25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9] | ([0-9])] {1,2}) {3)}}}}:)

In order to make the logic clear, we use the newline character and delete the newline character to get the final regular expression of IPv6

The [0-9a-fa-9a-9a-fa-fa-f-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa-fa– 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] [22 [0-3] [2 [0-1 [0-1] [0-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [1 [1 [1-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9 [- F] {1,4} {1,3}: ((22 [0-3] 2 [0-1] [0-9] | [0-1] [0-9] [0-9] | [1 – 9] [0-9] | ([0-9])]] {1,2}) ([.] (25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9] | [1 – 9] [0-9] | ([0-9])] {1, 2} The [0-9a-fa-fa-f [0-9a-9a-fa-f] {[1,4} {[0-9a-fa-f] {[0-9a-fa-f] {[1,4} {[1,4} {[1,4 {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4 {[1 [0 [0-9 [0-9 [0-9-9] [0-9] [0-9] [0-9-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-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-9] | ([0-9])] {1, 2} [0-1 [0-1] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9 [0-9 [0-9 [0-9 [0-9]) [[[25 [0 [0-5] [[2 [[[0-5-5] [[12 [[0-5-5-5] [2 [0-5-5-5-5] [2 [0-5-5-5] [2 [[[0-5-5-5-5-5-5-5] [12 [12 [12 [5-5-5-5-5-5-5-5-5] [12 [[[12 [12 [12 [12 [[[12 [[5]]]]]]] | ([0-9])] {1,2}) ([.] (25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9] | [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] | [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}) |:)

Add the first and last line matchers:

The [0-9a-fa-fa-fa-9a-9a-9a-fa-f] {[1,4 {[0-9a-fa-fa-f] {[1,4} {[0-9a-9a-fa-f] {[1,4 {[1,4 {[1,4}: {[7 [0-9a-9a-fa-f] {[0-9a-fa-fa-f] {[0-9a-9a-9a-fa-fa-f] {[1,4 {[1,4 {[1,4 {[1,4 {[1,4} [1,4 {[1,4 {[1,4} [1,4} [1} [1} [1} [1} [1,4 {[1} [1} [1 {[1} [1} [1 {[1 [1 [[0-4] [0-9] | [0-1] [0-9] [0-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] [22 [0-3] [2 [0-1 [0-1] [0-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [1 [1 [1-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9 [- F] {1,4} {1,3}: ((22 [0-3] 2 [0-1] [0-9] | [0-1] [0-9] [0-9] | [1 – 9] [0-9] | ([0-9])]] {1,2}) ([.] (25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9] | [1 – 9] [0-9] | ([0-9])] {1, 2} The [0-9a-fa-fa-f [0-9a-9a-fa-f] {[1,4} {[0-9a-fa-f] {[0-9a-fa-f] {[1,4} {[1,4} {[1,4 {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4 {[1 [0 [0-9 [0-9 [0-9-9] [0-9] [0-9] [0-9-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-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-9] | ([0-9])] {1, 2} [0-1 [0-1] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9 [0-9 [0-9 [0-9 [0-9]) [[[25 [0 [0-5] [[2 [[[0-5-5] [[12 [[0-5-5-5] [2 [0-5-5-5-5] [2 [0-5-5-5] [2 [[[0-5-5-5-5-5-5-5] [12 [12 [12 [5-5-5-5-5-5-5-5-5] [12 [[[12 [12 [12 [12 [[[12 [[5]]]]]]] | ([0-9])] {1,2}) ([.] (25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9] | [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] | [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}) |:) $)

There are some imperfections in the above expression: the first 80 bits of compatible address and mapping address must be 0, so the above range is actually wider, and makes the expression more complex. However, when we found this problem, there was no need to optimize it, so we need to optimize it by ourselves.

HTTPS

Regular:

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

Add the first and last line matchers:

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

domain name

Corresponding rules:

Not the last paragraph

a. The character range is a-za-z0-9 and dash-

b. Start and end characters cannot be empty-

c. Length not exceeding 63

2. Last paragraph

a. The character range is a-za-z

b. The length is 2-6

3. Not just 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 first and last line matchers:

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

Empty string

(^$)

Java: IPv4 and IPv6 regular test cases are attached

import org.junit.Test;

public class IPv6Test {
 Public static final string IPv4 regex = "(^ ((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}) $))";
 Public static final string IPv6 regex is the public static final string string in public, and it's the public static final string in public, and it's the final final string in public, and it's the final string in public, and it's the final final string in public, and it's the final goal of the final string in public, and it's the final goal of the six regex = "({([0-9a-fa-f] {[1,4}:) {[7 {[7 [0-9a-9a-9a-9a-fa-fa-f] {[1,4 {[1,4 {[1,4 {[1,4}]} {[1,4 {[1,4 {[1,4 {[1,4 {[1,4]} [1,4 [1,4]} [1 [1 [1,4]} [1 [1 [1 [1 [1 [1 [1 [1 [1 [1 [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] [22 [0-3] [2 [0-1 [0-1] [0-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [1 [1 [1-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9 [- F] {1,4} {1,3}: ((22 [0-3] 2 [0-1] [0-9] | [0-1] [0-9] [0-9] | [1 - 9] [0-9] | ([0-9])]] {1,2}) ([.] (25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9] | [1 - 9] [0-9] | ([0-9])] {1, 2} The [0-9a-fa-fa-f [0-9a-9a-fa-f] {[1,4} {[0-9a-fa-f] {[0-9a-fa-f] {[1,4} {[1,4} {[1,4 {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4} {[1,4 {[1 [0 [0-9 [0-9 [0-9-9] [0-9] [0-9] [0-9-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-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-9] | ([0-9])] {1, 2} [0-1 [0-1] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9] [0-9] [0-9] [0-9 [1-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9 [0-9] [0-9 [0-9 [0-9 [0-9 [0-9]) [[[25 [0 [0-5] [[2 [[[0-5-5] [[12 [[0-5-5-5] [2 [0-5-5-5-5] [2 [0-5-5-5] [2 [[[0-5-5-5-5-5-5-5] [12 [12 [12 [5-5-5-5-5-5-5-5-5] [12 [[[12 [12 [12 [12 [[[12 [[5]]]]]]] | ([0-9])] {1,2}) ([.] (25 [0-5] | 2 [0-4] [0-9] | [0-1] [0-9] [0-9] | [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] | [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) {
   e.printStackTrace(System.out);
  }

 }

 //The first test case with a field length of 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 ("https://a.com".matches(finalRegex));
  assert ("https://a.b.c.com".matches(finalRegex));
  assert ("https://a".matches(finalRegex));
  assert ("https://a.comdddd".matches(finalRegex));
  assert (!"https://afadfadfadfadfadfadfadfadfadfffffffffffffffffffffffffffffffffffffffffffffffdfadfadfadfadfadfadfadfaafadfadfadfadfadfadfadfadfadfffffffffffffffffffffffffffffffffffffffffffffffdfadfadfadfadfadfadfadfaafadfadfadfadfadfadfadfadfadfffffffffffffffffffffffffffffffffffffffffffffffdfadfadfadfadfadfadfadfa.comdddd"
    .matches(finalRegex));
 }

 @Test
 public void testDomain() {
  assert ("a.com".matches(finalRegex));
  assert ("a.bdfad-dfadf.c.com".matches(finalRegex));
  assert (!"a.-bdfad-dfadf.c.com".matches(finalRegex));
  assert ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijk.com".matches(finalRegex));
  assert (!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijk1.com".matches(finalRegex));
 }

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

 }
}

Here is the article about IPv4 and IPv6 regular expressions. For more information about IPv4 and IPv6 regular expressions, please search previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!