婷婷久久综合九色综合,欧美成色婷婷在线观看视频,偷窥视频一区,欧美日本一道道一区二区

<tt id="bu9ss"></tt>
  • <span id="bu9ss"></span>
  • <pre id="bu9ss"><tt id="bu9ss"></tt></pre>
    <label id="bu9ss"></label>

    當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

    IPV4和IPV6正則表達(dá)式的深入講解

     2020-12-15 15:50  來源: 腳本之家   我來投稿 撤稿糾錯(cuò)

      阿里云優(yōu)惠券 先領(lǐng)券再下單

    這篇文章主要給大家介紹了關(guān)于IPV4和IPV6正則表達(dá)式的相關(guān)資料,文中介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

    IPV4正則表達(dá)式

    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類地址。每個(gè)字節(jié)的第一個(gè)數(shù)字可以為0,比如說01, 001。

    1. ip的第一個(gè)字節(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}

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

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

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

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

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

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

    分為以下幾種情況:

    1. 以2開頭

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

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

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

    ([.]

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

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

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

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

    3. 加上點(diǎn)號和重復(fù)三次,以及開始和結(jié)尾匹配符,IPV4最終正則表達(dá)式變?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})

    點(diǎn)號之所以用中括號括起來是因?yàn)槿绻粩U(kuò)起來是匹配任意字符。也可以用兩個(gè)反斜桿轉(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正則表達(dá)式

    IPV6介紹

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

    IPv6地址表示

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

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

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

    AD80::ABAA:0000:00C2:0002

    這個(gè)簡化只能用一次,在上例中的ABAA后面的0000就不能再次簡化。當(dāng)然也可以在ABAA后面使用::,這樣的話前面的12個(gè)0就不能壓縮了。這個(gè)限制的目的是為了能準(zhǔn)確還原被壓縮的0,不然就無法確定每個(gè)::代表了多少個(gè)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::

    同時(shí)每個(gè)段前面的零可以省略,因此

    2001:0DB8:02de::0e13 等價(jià)于

    2001:DB8:2de::e13

    一個(gè)IPv6地址可以將一全I(xiàn)Pv4地址內(nèi)嵌進(jìn)去,寫成IPv6形式和平常習(xí)慣的IPv4形式的混合體。

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

    IPv4映像地址

    0000:0000:0000:0000:0000:ffff:192.168.89.9這種混合寫法對應(yīng)的ipv6地址:

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

    其實(shí)表示的是192.168.89.9這個(gè)ipv4地址。IPv4映像地址布局如下:

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

    IPv4兼容地址

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

    IPv4兼容地址布局如下:

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

    格式分為以下幾種情況:

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

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

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

    對應(yīng)的正則表達(dá)式為:

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

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

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

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

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

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

    對應(yīng)的正則表達(dá)式為:

    (([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個(gè)完整的字段,后面3個(gè)字段可能為零壓縮寫法或者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

    對應(yīng)的正則表達(dá)式為:

    (([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個(gè)完整的字段,后面4個(gè)字段可能為零壓縮寫法或者ipv4地址嵌入式寫法。如:

    1:2:3:4::

    1:2:3:4::5

    1:2:3:4::8

    1:2:3:4::192.168.1.1

    對應(yīng)的正則表達(dá)式為:

    (([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個(gè)完整字段的情況略。

    8. 第一個(gè)字段即開始簡略寫法

    ::8

    ::192.168.1.1

    對應(yīng)的正則表達(dá)式為:

    (:(:[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的正則表達(dá)式為:

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

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

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

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

    HTTPS

    正則:

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

    加上行首和行尾匹配符:

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

    域名

    對應(yīng)的規(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);
    }

    }

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

    }

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

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

    }
    }

    到此這篇關(guān)于IPV4和IPV6正則表達(dá)式的文章就介紹到這了,更多相關(guān)IPV4和IPV6正則表達(dá)式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

    來源:腳本之家

    鏈接:https://www.jb51.net/article/201691.htm

    申請創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

    相關(guān)文章

    熱門排行

    信息推薦