IPV4與IPV6正則表達式的區(qū)別是什么-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)IPV4與IPV6正則表達式的區(qū)別是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

我們提供的服務有:成都做網(wǎng)站、成都網(wǎng)站建設、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、田林ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的田林網(wǎng)站制作公司

IPV4正則表達式

Ipv4地址分為ABCDE五大類,其中ABC類是普通ip地址,D類是組播地址,E類保留,作為研究之用。

范圍分別為:

A: 1.0.0.1 ―一126.155.255.255

內(nèi)網(wǎng)地址范圍:10.0.0.0 一一10-255.255.255

B: 127.0.0.1 —191.255.255.255

內(nèi)網(wǎng)地址范圍:172.16.0.0——172.31.255.255

C: 192.0.0.1 —223.255.255.255

內(nèi)網(wǎng)地址范圍: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

我們的正則要求ip必須是ABC類地址。每個字節(jié)的第一個數(shù)字可以為0,比如說01, 001。

1.       ip的第一個字節(jié)分作以下幾種情況:

1.長度為3且以2開頭,范圍為200-223

正則:22[0-3]丨2[0-1][0-9]

2.長度為3且以0或1開頭

正則:[0-1][0-9][0-9]

3.長度為1或2

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

所以第一個字節(jié)的表達式為:

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

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

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

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

2.    后面三個字節(jié)范圍為0-255,前面有一個點

分為以下幾種情況:

1.    以2開頭

正則:25[0-5]|2[0-4][0-9]

2.    以1和0開頭的情況和第一個字節(jié)相同。

所以,單個字節(jié)的正則表達式:

([.]

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

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

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

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

3.    加上點號和重復三次,以及開始和結(jié)尾匹配符,IPV4最終正則表達式變?yōu)椋?/strong>

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

點號之所以用中括號括起來是因為如果不擴起來是匹配任意字符。也可以用兩個反斜桿轉(zhuǎn)義。

加上行首和行尾匹配符:

(^((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})$)

IPV6正則表達式

IPV6介紹

IPV6的長度是128位,相比于ipv4的32位,極大的擴展了ip地址可用空間。ipv4地址現(xiàn)在被視為一種稀缺資源,而ipv6地址相當充足,在可以預見的未來是用不完的。有這樣一段描述:如果地球表面(含陸地和水面)都覆蓋著計算機,那么IPv6允許每平方米擁有7*10A23個IP地址;如果地址分配的速率是每微秒100萬個,那么需要10A19年才能將所有的地址分配完畢。

IPv6地址表示

IPv6的128位地址通常寫成8組,每組為四個十六進制數(shù)的形式。比如:

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

是一個合法的IPv6地址。這個地址比較長,看起來不方便也不易于書寫。零壓縮法可以用來縮減其長度。如果幾個連續(xù)段位的值都是0,那么這些0就可以簡單的以::來表示,上述地址就可寫成:

AD80::ABAA:0000:00C2:0002

這個簡化只能用一次,在上例中的ABAA后面的0000就不能再次簡化。當然也可以在ABAA后面使用::,這樣的話前面的12個0就不能壓縮了。這個限制的目的是為了能準確還原被壓縮的0,不然就無法確定每個::代表了多少個0。例如,下面是一些合法的IPv6地址:

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

同時每個段前面的零可以省略,因此

2001:0DB8:02de::0e13 等價于

2001:DB8:2de::e13

一個IPv6地址可以將一全IPv4地址內(nèi)嵌進去,寫成IPv6形式和平常習慣的IPv4形式的混合體。

IPv6有兩種內(nèi)嵌IPv4的方式:IPv4映像地址和IPv4兼容地址(已經(jīng)被舍棄)。

IPv4映像地址

0000:0000:0000:0000:0000:ffff:192.168.89.9這種混合寫法對應的ipv6地址:

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

其實表示的是192.168.89.9這個ipv4地址。IPv4映像地址布局如下:

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

IPv4兼容地址


兼容地址和映像地址的區(qū)別就是第81-96位為0。


IPv4兼容地址布局如下:


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

格式分為以下幾種情況:

1.    前面7個完整的字段,第8個字段為零壓縮寫法或者數(shù)字。如:

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

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

對應的正則表達式為:

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

最外面加一層括號是為了保證與其他正則表達式拼接到一起時保證不產(chǎn)生混亂。

2.    前面6個完整的字段,后面2個字段可能為零壓縮寫法或者ipv4地址嵌入式寫法。如:

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

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

1:2:3:4:5:6:192.168.1.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})|:))

3.    前面5個完整的字段,后面3個字段可能為零壓縮寫法或者ipv4地址嵌入式寫法。如:

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

對應的正則表達式為:

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

4.    前面4個完整的字段,后面4個字段可能為零壓縮寫法或者ipv4地址嵌入式寫法。如:

1:2:3:4::

1:2:3:4::5

1:2:3:4::8

1:2:3:4::192.168.1.1

對應的正則表達式為:

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

前面3,2,1個完整字段的情況略。

8.    第一個字段即開始簡略寫法

::8

::192.168.1.1

對應的正則表達式為:

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

所以IPV6的正則表達式為:

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

上面為了表達邏輯的清晰,用了換行符,刪除換行符,便得到IPV6的最終正則表達式:

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

加上行首和行尾匹配符:

(^(([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})|:))$)

上面的表達式還有考慮的不完善的地方:兼容地址和映射地址的前80bit一定是0,所以上面的范圍其實寫的更寬了,而且使表達式變復雜了。但是發(fā)現(xiàn)這個問題的時候,已經(jīng)沒有優(yōu)化的需求了,所以就先這樣,有需要的自行進行優(yōu)化。

HTTPS

正則:

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

加上行首和行尾匹配符:

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

域名

對應的規(guī)則:

1.       非最后一段

a.    字符范圍為:a-zA-Z0-9以及短劃線-

b.    開始和結(jié)束字符不能為-

c.     長度不超過63

2.       最后一段

a.       字符范圍為a-zA-Z

b.       長度為2-6

3.    不能只有最后一段

正則:

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

加上行首和行尾匹配符:

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

空字符串

(^$)

附IPv4及IPv6正則測試用例——java:

import org.junit.Test;

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) {
   e.printStackTrace(System.out);
  }

 }

 // 第一個字段長度為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));

 }

 // 第一個字段長度為1或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));
 }

 // 測試后面三個字節(jié)
 @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
 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));

 }
}

文章題目:IPV4與IPV6正則表達式的區(qū)別是什么-創(chuàng)新互聯(lián)
本文來源:http://bm7419.com/article6/dicoig.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站建站公司、定制開發(fā)、用戶體驗關(guān)鍵詞優(yōu)化、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)