簡體   English   中英

使用正則表達式驗證 IPv4 地址

[英]Validating IPv4 addresses with regexp

我一直在嘗試為 IPv4 驗證獲取有效的正則表達式,但運氣不佳。 似乎在某一時刻我遇到過(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4} ,但它會產生一些奇怪的結果:

$ grep --version
grep (GNU grep) 2.7
$ grep -E '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4}\b' <<< 192.168.1.1
192.168.1.1
$ grep -E '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4}\b' <<< 192.168.1.255
192.168.1.255
$ grep -E '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4}\b' <<< 192.168.255.255
$ grep -E '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4}\b' <<< 192.168.1.2555
192.168.1.2555

我進行了搜索以查看是否已經提出並回答了這個問題,但其他答案似乎只是顯示了如何確定 4 組 1-3 數字,或者對我不起作用。

您已經得到了一個有效的答案,但以防萬一您對原始方法有什么問題感到好奇,答案是您需要在交替周圍加上括號,否則(\\.|$)僅在數字小於時才需要200。

'\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b'
    ^                                    ^
^((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]?)$

接受

127.0.0.1
192.168.1.1
192.168.1.255
255.255.255.255
0.0.0.0
1.1.1.01        # This is an invalid IP address!

拒絕

30.168.1.255.1
127.1
192.168.1.256
-1.2.3.4
1.1.1.1.
3...3

在線嘗試單元測試: https : //www.debuggex.com/r/-EDZOqxTxhiTncN6/1

最新、最短、最不可讀的版本( 49 個字符

^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}$

[0-9]塊可以在 2 個地方用\\d替換 - 使其可讀性稍差,但絕對更短。

更新、更短、可讀性第二低的版本( 55 個字符

^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\.(?!$)|$)){4}$

此版本查找 250-5 案例,然后巧妙地將200-249 100-199 10-99案例的所有可能案例進行 OR 200-249 請注意, |)部分不是錯誤,但實際上是對 0-9 范圍的最后一種情況進行 OR 運算。 我還省略了?:非捕獲組部分,因為我們並不真正關心捕獲的項目,如果我們一開始沒有完全匹配,它們就不會被捕獲。

舊的和較短的版本(可讀性較差)( 63 個字符

^(?:(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\\.(?!$)|$)){4}$

較舊的(可讀)版本( 70 個字符

^(?:(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(\\.(?!$)|$)){4}$

它使用負前瞻(?!)來刪除 ip 可能以.

最舊的答案( 115 個字符

^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}
    (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$

我認為這是最准確和最嚴格的正則表達式,它不接受像000.021.01.0.這樣的000.021.01.0. 似乎這里的大多數其他答案都需要額外的正則表達式來拒絕與該類似的情況 - 即0起始數字和以.

正則表達式不是這項工作的工具。 編寫一個分隔四個數字的解析器並檢查它們是否在[0,255]范圍內會更好。 非功能性正則表達式已經不可讀了!

IPv4 地址(准確捕獲) 匹配 0.0.0.0 到 255.255.255.255,但會捕獲無效地址,例如 1.1.000.1 使用此正則表達式准確匹配 IP 號碼。 4 個數字中的每一個都存儲在一個捕獲組中,因此您可以訪問它們以進行進一步處理。

\b
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
\b

取自 JGsoft RegexBuddy 庫

編輯:這個(\\.|$)部分看起來很奇怪

我正在為 IPv4 地址尋找類似的東西 - 一個正則表達式,它也阻止了常用的私有 ip 地址被驗證(192.168.xy、10.xyz、172.16.xy),因此使用了負面預測來實現這一點:

(?!(10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.).*)
(?!255\.255\.255\.255)(25[0-5]|2[0-4]\d|[1]\d\d|[1-9]\d|[1-9])
(\.(25[0-5]|2[0-4]\d|[1]\d\d|[1-9]\d|\d)){3}

(這些當然應該在一行上,為了便於閱讀而格式化為 3 行)正則表達式可視化

調試器演示

它可能沒有針對速度進行優化,但在僅查找“真實”互聯網地址時效果很好。

將會(並且應該)失敗的事情:

0.1.2.3         (0.0.0.0/8 is reserved for some broadcasts)
10.1.2.3        (10.0.0.0/8 is considered private)
172.16.1.2      (172.16.0.0/12 is considered private)
172.31.1.2      (same as previous, but near the end of that range)
192.168.1.2     (192.168.0.0/16 is considered private)
255.255.255.255 (reserved broadcast is not an IP)
.2.3.4
1.2.3.
1.2.3.256
1.2.256.4
1.256.3.4
256.2.3.4
1.2.3.4.5
1..3.4

將(並且應該)工作的 IP:

1.0.1.0         (China)
8.8.8.8         (Google DNS in USA)
100.1.2.3       (USA)
172.15.1.2      (USA)
172.32.1.2      (USA)
192.167.1.2     (Italy)

以防其他人正在尋找驗證“不包括公共私有地址的 Internet IP 地址”

我認為許多閱讀這篇文章的人會尋找更簡單的正則表達式,即使它們匹配一些技術上無效的 IP 地址。 (而且,正如其他地方所述,正則表達式可能不是正確驗證 IP 地址的正確工具。)

刪除^並在適用的情況下將$替換$ \\b ,如果您不想匹配行的開頭/結尾。

基本正則表達式 (BRE)(在 GNU grep、GNU sed 和 vim 上測試):

/^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+$/

擴展正則表達式 (ERE):

/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/

或者:

/^([0-9]+(\.|$)){4}/

Perl 兼容的正則表達式 (PCRE)(在 Perl 5.18 上測試):

/^\d+\.\d+\.\d+\.\d+$/

或者:

/^(\d+(\.|$)){4}/

Ruby(在 Ruby 2.1 上測試):

盡管應該是 PCRE,但無論出於何種原因,Ruby 都允許使用 Perl 5.18 不允許的正則表達式:

/^(\d+[\.$]){4}/

我對所有這些的測試都 在這里在線。

上面的答案是有效的,但是如果 ip 地址不在行尾而是在文本之間怎么辦。這個正則表達式甚至可以解決這個問題。

代碼: '\\b((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\\.)){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\\b'

輸入文本文件:

ip address 0.0.0.0 asfasf
 sad sa 255.255.255.255 cvjnzx
zxckjzbxk  999.999.999.999 jshbczxcbx
sjaasbfj 192.168.0.1 asdkjaksb
oyo 123241.24121.1234.3423 yo
yo 0000.0000.0000.0000 y
aw1a.21asd2.21ad.21d2
yo 254.254.254.254 y0
172.24.1.210 asfjas
200.200.200.200
000.000.000.000
007.08.09.210
010.10.30.110

輸出文本:

0.0.0.0
255.255.255.255
192.168.0.1
254.254.254.254
172.24.1.210
200.200.200.200

這是一個更好的,附加了通過/失敗的 IP

/^((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/

接受

127.0.0.1
192.168.1.1
192.168.1.255
255.255.255.255
10.1.1.1
0.0.0.0

拒絕

1.1.1.01
30.168.1.255.1
127.1
192.168.1.256
-1.2.3.4
1.1.1.1.
3...3
192.168.1.099

這比一些長一點,但這是我用來匹配 IPv4 地址的方法。 簡單而不妥協。

^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$

''' 這段代碼對我有用,就這么簡單。

在這里,我采用了 ip 的值,並嘗試將其與正則表達式匹配。

ip="25.255.45.67"    

op=re.match('(\d+).(\d+).(\d+).(\d+)',ip)

if ((int(op.group(1))<=255) and (int(op.group(2))<=255) and int(op.group(3))<=255) and (int(op.group(4))<=255)):

print("valid ip")

else:

print("Not valid")

以上條件檢查所有 4 個八位字節的值是否超過 255,則它是無效的。 但是在應用條件之前,我們必須將它們轉換為整數,因為值在字符串中。

group(0) 打印匹配的輸出,而 group(1) 打印第一個匹配的值,這里是“25”,依此類推。 '''

(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2})))

測試以在文本中查找匹配項, https://regex101.com/r/9CcMEN/2

以下是定義每個 IP 地址數字中的有效組合的規則:

  • 任何一位或兩位數字。
  • 任何以1開頭的三位數。

  • 任何以2開頭的三位數,如果第二位數是04

  • 任何以25開頭的三位數字,如果第三位是05

讓我們從(((25[0-5])|(2[0-4]\\d)|(1\\d{2})|(\\d{1,2}))\\.) ,一個一組四個嵌套的子表達式,我們將按相反的順序查看它們。 (\\d{1,2})匹配任何一位或兩位數字或數字099 (1\\d{2})匹配任何以1開頭的三位數字( 1后跟任意兩位數字),或數字100199 (2[0-4]\\d)匹配數字200249 (25[0-5])匹配數字250255 這些子表達式中的每一個都包含在另一個帶有|子表達式中| 每個之間(這樣四個子表達式之一必須匹配,而不是全部)。 在數字范圍之后是\\. 匹配. ,然后整個系列(所有數字選項加上\\. )被包含在另一個子表達式中,並使用{3}重復三次。 最后,重復數字范圍(這次沒有尾隨\\. )以匹配最終的 IP 地址編號。 通過將四個數字中的每一個限制為0255之間的值,該模式確實可以匹配有效的 IP 地址並拒絕無效的地址。

摘自: Ben Forta。 “學習正則表達式。”


如果 IP 地址的開頭和結尾都不$字符,則應分別使用^$元字符。

^(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.){3}(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2})))$

測試以在文本中查找匹配項, https://regex101.com/r/uAP31A/1

/^(?:(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?1)$/m

對於從 0 到 255 的數字,我使用這個正則表達式:

(([0-9])|([1-9][0-9])|(1([0-9]{2}))|(2[0-4][0-9])|(25[0-5]))

以上正則表達式將匹配從 0 到 255 的整數,但不匹配 256。

所以對於 IPv4 我使用這個正則表達式:

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

它在這個結構中: ^(N)((\\.(N)){3})$其中 N 是用於匹配從 0 到 255 的數字的正則表達式。
此正則表達式將匹配 IP,如下所示:

0.0.0.0
192.168.1.2

但不是下面的那些:

10.1.0.256
1.2.3.
127.0.1-2.3

對於 IPv4 CIDR(無類別域間路由),我使用以下正則表達式:

^(([0-9])|([1-9][0-9])|(1([0-9]{2}))|(2[0-4][0-9])|(25[0-5]))((\.(([0-9])|([1-9][0-9])|(1([0-9]{2}))|(2[0-4][0-9])|(25[0-5]))){3})\/(([0-9])|([12][0-9])|(3[0-2]))$

它在這個結構中: ^(N)((\\.(N)){3})\\/M$其中 N 是用於匹配 0 到 255 數字的正則表達式,M 是用於匹配數字的正則表達式0 到 32。
此正則表達式將匹配 CIDR,如下所示:

0.0.0.0/0
192.168.1.2/32

但不是下面的那些:

10.1.0.256/16
1.2.3./24
127.0.0.1/33

對於像"10.0.0.0/16", "192.168.1.1/32"這樣的 IPv4 CIDR 列表,我使用這個正則表達式:

^("(([0-9])|([1-9][0-9])|(1([0-9]{2}))|(2[0-4][0-9])|(25[0-5]))((\.(([0-9])|([1-9][0-9])|(1([0-9]{2}))|(2[0-4][0-9])|(25[0-5]))){3})\/(([0-9])|([12][0-9])|(3[0-2]))")((,([ ]*)("(([0-9])|([1-9][0-9])|(1([0-9]{2}))|(2[0-4][0-9])|(25[0-5]))((\.(([0-9])|([1-9][0-9])|(1([0-9]{2}))|(2[0-4][0-9])|(25[0-5]))){3})\/(([0-9])|([12][0-9])|(3[0-2]))"))*)$

它在這個結構中: ^(“C”)((,([ ]*)(“C”))*)$其中 C 是用於匹配 CIDR 的正則表達式(如 0.0.0.0/0)。
此正則表達式將匹配 CIDR 列表,如下所示:

“10.0.0.0/16”,”192.168.1.2/32”, “1.2.3.4/32”

但不是下面的那些:

“10.0.0.0/16” 192.168.1.2/32 “1.2.3.4/32”

也許它可能會變得更短,但對我來說,我很容易理解它。

希望能幫助到你!

我設法從所有其他答案中構建了一個正則表達式。

(25[0-5]|2[0-4][0-9]|[1][0-9][0-9]|[1-9][0-9]|[0-9]?)(\.(25[0-5]|2[0-4][0-9]|[1][0-9][0-9]|[1-9][0-9]|[0-9]?)){3}

IPv4地址是一件非常復雜的事情。

注意:縮進和內襯僅用於說明目的,在真正的 RegEx 中不存在。

\b(
  ((
    (2(5[0-5]|[0-4][0-9])|1[0-9]{2}|[1-9]?[0-9])
  |
    0[Xx]0*[0-9A-Fa-f]{1,2}
  |
    0+[1-3]?[0-9]{1,2}
  )\.){1,3}
  (
    (2(5[0-5]|[0-4][0-9])|1[0-9]{2}|[1-9]?[0-9])
  |
    0[Xx]0*[0-9A-Fa-f]{1,2}
  |
    0+[1-3]?[0-9]{1,2}
  )
|
  (
    [1-3][0-9]{1,9}
  |
    [1-9][0-9]{,8}
  |
    (4([0-1][0-9]{8}
      |2([0-8][0-9]{7}
        |9([0-3][0-9]{6}
          |4([0-8][0-9]{5}
            |9([0-5][0-9]{4}
              |6([0-6][0-9]{3}
                |7([0-1][0-9]{2}
                  |2([0-8][0-9]{1}
                    |9([0-5]
    ))))))))))
  )
|
  0[Xx]0*[0-9A-Fa-f]{1,8}
|
  0+[1-3]?[0-7]{,10}
)\b

這些 IPv4 地址由上述 RegEx 驗證。

127.0.0.1
2130706433
0x7F000001
017700000001
0x7F.0.0.01 # Mixed hex/dec/oct
000000000017700000001 # Have as many leading zeros as you want
0x0000000000007F000001 # Same as above
127.1
127.0.1

這些都被拒絕了。

256.0.0.1
192.168.1.099 # 099 is not a valid number
4294967296 # UINT32_MAX + 1
0x100000000
020000000000

帶子網掩碼:

^$|([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\
.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\
.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\
.([01]?\\d\\d?|2[0-4]\\d|25[0-5])
((/([01]?\\d\\d?|2[0-4]\\d|25[0-5]))?)$

我試圖讓它更簡單和更短。

^(([01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d{1,2}|2[0-4]\\d|25[0-5])$

如果您正在尋找 java/kotlin:

^(([01]?\\\\d{1,2}|2[0-4]\\\\d|25[0-5])\\\\.){3}([01]?\\\\d{1,2}|2[0-4]\\\\d|25[0-5])$

如果有人想知道它是如何工作的,這里是解釋。 這真的很簡單。 試試看:p:

 1. ^.....$: '^' is the starting and '$' is the ending.

 2. (): These are called a group. You can think of like "if" condition groups.

 3. |: 'Or' condition - as same as most of the programming languages.

 4. [01]?\d{1,2}: '[01]' indicates one of the number between 0 and 1. '?' means '[01]' is optional. '\d' is for any digit between 0-9 and '{1,2}' indicates the length can be between 1 and 2. So here the number can be 0-199.

 5. 2[0-4]\d: '2' is just plain 2. '[0-4]' means a number between 0 to 4. '\d' is for any digit between 0-9. So here the number can be 200-249.

 6. 25[0-5]: '25' is just plain 25. '[0-5]' means a number between 0 to 5. So here the number can be 250-255.

 7. \.: It's just plan '.'(dot) for separating the numbers.

 8. {3}: It means the exact 3 repetition of the previous group inside '()'.

 9. ([01]?\d{1,2}|2[0-4]\d|25[0-5]): Totally same as point 2-6

在數學上它是這樣的:

(0-199 OR 200-249 OR 250-255).{Repeat exactly 3 times}(0-199 OR 200-249 OR 250-255)

因此,正如您通常看到的,這是 IP 地址的模式。 我希望它有助於理解正則表達式。 :p

我試圖讓它更簡單和更短。

^(([01]?\\d{1,2}|2[0-4]\\d|25[0-5]).){3}([01]?\\d{1,2}|2 [0-4]\\d|25[0-5])$

如果您正在尋找 java/kotlin:

^(([01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d{1,2}| 2[0-4]\\d|25[0-5])$

如果有人想知道它是如何工作的,這里是解釋。 這真的很簡單。 試試看:p:

 1. ^.....$: '^' is the starting and '$' is the ending.

 2. (): These are called a group. You can think of like "if" condition groups.

 3. |: 'Or' condition - as same as most of the programming languages.

 4. [01]?\d{1,2}: '[01]' indicates one of the number between 0 and 1. '?' means '[01]' is optional. '\d' is for any digit between 0-9 and '{1,2}' indicates the length can be between 1 and 2. So here the number can be 0-199.

 5. 2[0-4]\d: '2' is just plain 2. '[0-4]' means a number between 0 to 4. '\d' is for any digit between 0-9. So here the number can be 200-249.

 6. 25[0-5]: '25' is just plain 25. '[0-5]' means a number between 0 to 5. So here the number can be 250-255.

 7. \.: It's just plan '.'(dot) for separating the numbers.

 8. {3}: It means the exact 3 repetition of the previous group inside '()'.

 9. ([01]?\d{1,2}|2[0-4]\d|25[0-5]): Totally same as point 2-6

在數學上它是這樣的:

(0-199 OR 200-249 OR 250-255).{Repeat exactly 3 times}(0-199 OR 200-249 OR 250-255)

因此,正如您通常看到的,這是 IP 地址的模式。 我希望它有助於理解正則表達式。 :p

    const char*ipv4_regexp = "\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
    "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
    "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\."
    "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";

我將從 JGsoft RegexBuddy 庫中獲取的正則表達式改編為 C 語言(regcomp/regexec),我發現它可以工作,但在某些操作系統(如 Linux)中存在一些小問題。 該正則表達式接受像 192.168.100.009 這樣的 ipv4 地址,其中 009 在 Linux 中被視為八進制值,因此該地址不是您所想的。 我改變了正則表達式如下:

    const char* ipv4_regex = "\\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\."
           "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\."
           "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\."
           "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\b";

現在使用該正則表達式 192.168.100.009 不是有效的 ipv4 地址,而 192.168.100.9 可以。

我也修改了多播地址的正則表達式,如下所示:

    const char* mcast_ipv4_regex = "\\b(22[4-9]|23[0-9])\\."
                        "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\."
                        "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]?)\\."
                        "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\b";

我認為您必須使正則表達式適應您用於開發應用程序的語言

我在java中舉了一個例子:

    package utility;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class NetworkUtility {

        private static String ipv4RegExp = "\\b(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d?)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d?)\\b";

        private static String ipv4MulticastRegExp = "2(?:2[4-9]|3\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d?|0)){3}";

        public NetworkUtility() {

        }

        public static boolean isIpv4Address(String address) {
            Pattern pattern = Pattern.compile(ipv4RegExp);
            Matcher matcher = pattern.matcher(address);

            return matcher.matches();
        }

        public static boolean isIpv4MulticastAddress(String address) {
             Pattern pattern = Pattern.compile(ipv4MulticastRegExp);
             Matcher matcher = pattern.matcher(address);

             return matcher.matches();
        }
    }
-bash-3.2$ echo "191.191.191.39" | egrep 
  '(^|[^0-9])((2([6-9]|5[0-5]?|[0-4][0-9]?)?|1([0-9][0-9]?)?|[3-9][0-9]?|0)\.{3}
     (2([6-9]|5[0-5]?|[0-4][0-9]?)?|1([0-9][0-9]?)?|[3-9][0-9]?|0)($|[^0-9])'

>> 191.191.191.39

(這是一個匹配整個地址空間(包括廣播等)的 DFA,沒有別的。

我認為這是最短的。

^(([01]?\d\d?|2[0-4]\d|25[0-5]).){3}([01]?\d\d?|2[0-4]\d|25[0-5])$

我發現這個示例非常有用,而且它允許不同的 ipv4 符號。

使用python的示例代碼:

    def is_valid_ipv4(ip4):
    """Validates IPv4 addresses.
    """
    import re
    pattern = re.compile(r"""
        ^
        (?:
          # Dotted variants:
          (?:
            # Decimal 1-255 (no leading 0's)
            [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}
          |
            0x0*[0-9a-f]{1,2}  # Hexadecimal 0x0 - 0xFF (possible leading 0's)
          |
            0+[1-3]?[0-7]{0,2} # Octal 0 - 0377 (possible leading 0's)
          )
          (?:                  # Repeat 0-3 times, separated by a dot
            \.
            (?:
              [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2}
            |
              0x0*[0-9a-f]{1,2}
            |
              0+[1-3]?[0-7]{0,2}
            )
          ){0,3}
        |
          0x0*[0-9a-f]{1,8}    # Hexadecimal notation, 0x0 - 0xffffffff
        |
          0+[0-3]?[0-7]{0,10}  # Octal notation, 0 - 037777777777
        |
          # Decimal notation, 1-4294967295:
          429496729[0-5]|42949672[0-8]\d|4294967[01]\d\d|429496[0-6]\d{3}|
          42949[0-5]\d{4}|4294[0-8]\d{5}|429[0-3]\d{6}|42[0-8]\d{7}|
          4[01]\d{8}|[1-3]\d{0,9}|[4-9]\d{0,8}
        )
        $
    """, re.VERBOSE | re.IGNORECASE)
    return pattern.match(ip4) <> None
((\.|^)(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0$)){4}

此正則表達式不接受 08.8.8.8 或 8.08.8.8 或 8.8.08.8 或 8.8.8.08

只要 IP 包含數字以外的任何字符(IP 的后面或前面),就可以查找有效的 IP 地址。 創建了 4 個反向引用:$+{first}.$+{second}.$+{third}.$+{forth}

Find String:
#any valid IP address
(?<IP>(?<![\d])(?<first>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<second>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<third>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<forth>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))(?![\d]))
#only valid private IP address RFC1918
(?<IP>(?<![\d])(:?(:?(?<first>10)[\.](?<second>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5])))|(:?(?<first>172)[\.](?<second>(:?1[6-9])|(:?2[0-9])|(:?3[0-1])))|(:?(?<first>192)[\.](?<second>168)))[\.](?<third>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<forth>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))(?![\d]))

Notepad++ Replace String Option 1: Replaces the whole IP (NO Change):
$+{IP}

Notepad++ Replace String Option 2: Replaces the whole IP octect by octect (NO Change)
$+{first}.$+{second}.$+{third}.$+{forth}

Notepad++ Replace String Option 3: Replaces the whole IP octect by octect (replace 3rd octect value with 0)
$+{first}.$+{second}.0.$+{forth}
NOTE: The above will match any valid IP including 255.255.255.255 for example and change it to 255.255.0.255 which is wrong and not very useful of course.

用實際值替換每個八位字節的一部分,但是您可以構建自己的查找和替換,這對於修改文本文件中的 IP 非常有用:

for example replace the first octect group of the original Find regex above:
(?<first>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))
with
(?<first>10)

and
(?<second>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))
with
(?<second>216)
and you are now matching addresses starting with first octect 192 only

Find on notepad++:
(?<IP>(?<![\d])(?<first>10)[\.](?<second>216)[\.](?<third>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<forth>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))(?![\d]))

您仍然可以以與以前完全相同的方式使用反向引用組執行替換。

您可以了解上面的匹配方式:

cat ipv4_validation_test.txt
Full Match:
0.0.0.1
12.108.1.34
192.168.1.1
10.249.24.212
10.216.1.212
192.168.1.255
255.255.255.255
0.0.0.0


Partial Match (IP Extraction from line)
30.168.1.0.1
-1.2.3.4
sfds10.216.24.23kgfd
da11.15.112.255adfdsfds
sfds10.216.24.23kgfd


NO Match
1.1.1.01
3...3
127.1.
192.168.1..
192.168.1.256
da11.15.112.2554adfdsfds
da311.15.112.255adfdsfds

使用 grep 可以看到以下結果:

From grep:
grep -oP '(?<IP>(?<![\d])(?<first>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<second>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<third>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<forth>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))(?![\d]))' ipv4_validation_test.txt
0.0.0.1
12.108.1.34
192.168.1.1
10.249.24.212
10.216.1.212
192.168.1.255
255.255.255.255
0.0.0.0
30.168.1.0
1.2.3.4
10.216.24.23
11.15.112.255
10.216.24.23


grep -P '(?<IP>(?<![\d])(?<first>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<second>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<third>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<forth>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))(?![\d]))' ipv4_validation_test.txt
0.0.0.1
12.108.1.34
192.168.1.1
10.249.24.212
10.216.1.212
192.168.1.255
255.255.255.255
0.0.0.0
30.168.1.0.1
-1.2.3.4
sfds10.216.24.23kgfd
da11.15.112.255adfdsfds
sfds10.216.24.23kgfd


#matching ip addresses starting with 10.216
grep -oP '(?<IP>(?<![\d])(?<first>10)[\.](?<second>216)[\.](?<third>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))[\.](?<forth>(:?\d)|(:?[1-9]\d)|(:?1\d{2})|(:?2[0-4]\d)|(:?25[0-5]))(?![\d]))' ipv4_validation_test.txt
10.216.1.212
10.216.24.23
10.216.24.23

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


以上將是 IP 地址的正則表達式,例如:221.234.000.112 也適用於 221.234.0.112、221.24.03.112、221.234.0.1


你可以想象上面的各種地址

我會使用 PCRE 和define關鍵字:

/^
 ((?&byte))\.((?&byte))\.((?&byte))\.((?&byte))$
 (?(DEFINE)
     (?<byte>25[0-5]|2[0-4]\d|[01]?\d\d?))
/gmx

演示: https : //regex101.com/r/IB7j48/2

這樣做的原因是為了避免重復(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)模式四次。 其他解決方案(例如下面的解決方案)運行良好,但它並沒有像許多人要求的那樣捕獲每個組。

/^((\d+?)(\.|$)){4}/ 

擁有 4 個捕獲組的唯一其他方法是重復該模式四次:

/^(?<one>\d+)\.(?<two>\d+)\.(?<three>\d+)\.(?<four>\d+)$/

因此,在 perl 中捕獲 ipv4 非常容易

$ echo "Hey this is my IP address 138.131.254.8, bye!" | \
  perl -ne 'print "[$1, $2, $3, $4]" if \
    /\b((?&byte))\.((?&byte))\.((?&byte))\.((?&byte))
     (?(DEFINE)
        \b(?<byte>25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))
    /x'

[138, 131, 254, 8]

我能想象到的最精確、直接和緊湊的 IPv4 正則表達式是

^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$

但是...的性能/效率如何呢?對不起,我不知道,誰在乎呢?

嘗試這個:

\b(([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.(2[0-5][0-5]|1[0-9][0-9]|[1-9][0-9]|[1-9]))\b
ip address can be from 0.0.0.0 to 255.255.255.255

(((0|1)?[0-9][0-9]?|2[0-4][0-9]|25[0-5])[.]){3}((0|1)?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$

(0|1)?[0-9][0-9]? - checking value from 0 to 199
2[0-4][0-9]- checking value from 200 to 249
25[0-5]- checking value from 250 to 255
[.] --> represent verify . character 
{3} --> will match exactly 3
$ --> end of string

以下是驗證 IP 地址的正則表達式。

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

我一直在嘗試獲得用於IPv4驗證的高效正則表達式,但運氣不佳。 似乎我曾經有過(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\\.|$)){4} ,但會產生一些奇怪的結果:

$ grep --version
grep (GNU grep) 2.7
$ grep -E '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4}\b' <<< 192.168.1.1
192.168.1.1
$ grep -E '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4}\b' <<< 192.168.1.255
192.168.1.255
$ grep -E '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4}\b' <<< 192.168.255.255
$ grep -E '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?(\.|$)){4}\b' <<< 192.168.1.2555
192.168.1.2555

我進行了搜索以查看是否已被問到並回答過,但是其他答案似乎只是顯示了如何確定4組1-3個數字,或者對我不起作用。

這個只匹配有效的IP(沒有前置的0,但它將匹配0-255的八位字節,無論它們的'功能'[即保留,私有等])並允許內聯匹配,其中可能有空格和/或在IP之后,或使用CIDR表示法時。

grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)'

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< '10.0.1.2'
10.0.1.2

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< 'ip address 10.0.1.2'
ip address 10.0.1.2

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< 'ip address 10.0.1.2 255.255.255.255'
ip address 10.0.1.2 255.255.255.255

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< 'ip address 10.0.1.2/32'
ip address 10.0.1.2/32

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< 'ip address 10.0.1.2.32'
$

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< 'ip address10.0.1.2'
$

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< '10.0.1.256'
$

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< '0.0.0.0'
0.0.0.0

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< '255.255.255.255'
255.255.255.255

$ grep -E '(^| )((([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])($| |/)' <<< '255.255.255.256'
$

當然,在IP是內聯的情況下,如果你只想要整個IP而且只需要IP,你可以使用grep選項“-o”和你喜歡的空白修剪器。

對於我們這些使用python的人來說,大致相當於:

>>> ipv4_regex = re.compile(r'(^| )((?:[1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:[1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])($| |/)')
>>> ipv4_regex.search('ip address 10.1.2.3/32')
<re.Match object; span=(10, 20), match=' 10.1.2.3/'>

如果你像我一樣挑剔(懶惰),你可能更願意使用分組來獲得整個IP,除了IP或CIDR之外什么都沒有,除了CIDR或其中的一些組合。 我們可以使用(?P)語法來命名我們的組以便於參考。

>>> ipv4_regex = re.compile(r'(?:^| )(?P<address>((?:[1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:[1-9]?\d|1\d{2}|2[0-4]\d|25[0-5]))(?P<slash>/)?(?(slash)(?P<cidr>[0-9]|[12][0-9]|3[0-2]))(?:$| )')
>>> match = ipv4_regex.search('ip address 10.0.1.2/32')
>>> match.group('address')
'10.0.1.2'
>>> match.group('cidr')
'32'
>>> "".join((match.group('address'), match.group('slash'), match.group('cidr')))
'10.0.1.2/32'

當然,還有不使用正則表達式的方法。 這里有一些你可以檢查的條件(這個沒有找到內聯,只是驗證傳遞的地址是有效的)。

首先檢查地址中的每個字符是數字還是'。'

接下來檢查確切的是3'。'

接下來的兩個檢查檢查每個八位字節是否在0到255之間。

最后一項檢查是沒有八位字節前置'0'

def validate_ipv4_address(address):
    return all(re.match('\.|\d', c) for c in address) \
        and address.count('.') == 3 \
        and all(0 <= int(octet) <= 255 for octet in address.split('.')) \
        and all((len(bin(int(octet))) <= 10 for octet in address.split('.'))) \
        and all(len(octet) == 1 or d[0] != '0' for octet in address.split('.'))


>>> validate_ipv4_address('255.255.255.255')
True
>>> validate_ipv4_address('10.0.0.1')
True
>>> validate_ipv4_address('01.01.01.01')
False
>>> validate_ipv4_address('123.456.789.0')
False
>>> validate_ipv4_address('0.0.0.0')
True
>>> validate_ipv4_address('-1.0.0.0')
False
>>> validate_ipv4_address('1.1.1.')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in validate_ipv4_address
  File "<stdin>", line 4, in <genexpr>
ValueError: invalid literal for int() with base 10: ''
>>> validate_ipv4_address('.1.1.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in validate_ipv4_address
  File "<stdin>", line 4, in <genexpr>
ValueError: invalid literal for int() with base 10: ''
>>> validate_ipv4_address('1..1.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in validate_ipv4_address
  File "<stdin>", line 4, in <genexpr>
ValueError: invalid literal for int() with base 10: ''

(按位,每個八位字節應為8位或更少,但每個八位字節前面加上'0b')

>>> bin(0)
'0b0'
>>> len(bin(0))
3
>>> bin(255)
'0b11111111'
>>> len(bin(256))
11

我在這個頁面看到非常糟糕的正則表達式..所以我帶着自己的:

\b((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\b

說明:

num-group = (0-9|10-99|100-199|200-249|250-255)
<border> + { <num-group> + <dot-cahracter> }x3 + <num-group> + <border>

在這里,你可以驗證它是如何工作在這里

適用於 Java 的 IPV4 地址的有效正則表達式

^((\\d|[1-9]\\d|[0-1]\\d{2}|2[0-4]\\d|25[0-5])[\\.]){3}(\\d|[1-9]\\d|[0-1]\\d{2}|2[0-4]\\d|25[0-5])$

我的 [擴展] 方法 → 空格分隔 IP 地址的正則表達式:

((((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\\\\.(?=\\\\d)|(?!\\\\d))){4})( (?!$)|$))+

使用 PCRE 前瞻。

嘗試這個

^(127|10).[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"

此模式不包括 52 個符號並接受以零開頭的 cucks。

/^(?:(?:[01]?\d?\d|2[0-4]\d|25[0-5])(?:\.|$)){4}\b$/

我的解決方案在這里:

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

此正則表達式將匹配:

0.0.0.0
255.255.255.255
123.123.123.123
127.0.0.1
192.168.0.1

但它不會匹配:

192.168.1.01
256.256.256.256
01.01.01.01

要驗證 0.0.0.0 到 255.255.255.255 有效范圍內的任何 IP 地址,可以寫成如下非常簡單的形式。

((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])

在文中找到一個有效的ip地址是一個非常困難的問題


我有一個正則表達式,它從文本文件中的字符串中匹配(提取)有效的 IP 地址。

我的正則表達式

\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){2}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\b
  • \\b詞邊界
  • (?: - 表示啟動非捕獲組
  • ^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\\.) - 字符串必須以帶點字符的第一個右八位字節開始
    • (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]| [1-9] ) - 找到第一個正確的八位字節 - (第一個八位字節不能以 - 0 開頭)
  • (?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){2} - 找到右下兩個帶點串的八位字節
  • (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\b - 字符串必須以右第四個八位字節結尾(現在允許零字符)

但是這個 ip regexp 有少數誤報匹配:

https://regexr.com/69dk7

正則表達式用於大多數 ip 地址的正確匹配

僅使用正則表達式從文本文件中查找或提取有效的 IP 地址是不可能的。 如果不檢查其他條件,您總是會得到誤報匹配。

解決方案


我寫了一個 liner perl來從文本文件中提取 ip 地址。 它有這樣的條件:

  • 當 ip 地址在行首時,下一個字符是一個或多個空白字符(空格、制表符、換行符...)
  • 當ip地址在行尾時,新行是下一個字符,ip地址之前是一個或多個空格字符
  • 在文本中間 - ip 地址前后是一個或多個空格字符

結果是 perl 不匹配像https://84.25.74.125這樣的字符串和另一個 URI 字符串。 或者 ip 地址在行尾,點字符在末尾。 但它會在文本中找到任何有效的 IP 地址。

perl 一個班輪解決方案:

$ cat ip.txt | perl -lane 'use warnings; use strict; for my $i (@F){if ($i =~/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){2}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/) { print $i; } }'
36.42.84.233
158.22.45.0
36.84.84.233
12.0.5.4
1.25.45.36
255.3.6.5
4.255.2.1
127.0.0.1
127.0.0.5
126.0.0.1

測試文本文件:

$ cat ip.txt
36.42.84.233 stop 158.22.45.0 and 56.32.58.2.
25.36.84.84abc and abc2.4.8.2 is error.
1.2.3.4_
But false positive is 2.2.2.2.2.2.2.2 or 1.1.1.1.1
http://23.54.212.1:80
https://89.35.248.1/abc
36.84.84.233 was 25.36.58.4/abc/xyz&158.133.26.4&another_var
and 42.27.0.1:8333 in http://212.158.45.2:26
0.25.14.15 ip can not start with zero
2.3.0
abc 12.0.5.4
1.25.45.36
12.05.2.5
256.1.2.5
255.3.6.5
4.255.2.1
4.256.5.6
127.0.0.1 is localhost.
this ip 127.0.0.5 is not localhost
126.0.0.1

附錄


對於來自其他星球的人來說,字符串2130706433 , 127.1 , 24.005.04.52是一個有效的 IP 地址 我有一條消息:嘗試自己找到解決方案!!!

考慮到建議的一些變體, \d\b可能不受支持。 因此,以防萬一:

IPv4 地址

^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)$

測試: https://debuggex.com/r/izHiog3KkYztRMSJ

圖形

這是正則表達式對我有用:
"\\<((([1-9]|1[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([1-9]|1[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]))\\>"

String zeroTo255 = "([0-9]|[0-9][0-9]|(0|1)[0-9][0-9]|2[0-4][0-9]|25[0-5])";

it can contain single digit i.e ([0-9]);  
It can contain two digits i.e ([0-9][0-9]); 
range is (099 to 199)i.e((0|1)[0-9][0-9]); 
range is (200 - 249) i.e (2[0-9][0-9]) ; 
range is (250-255) i.e(25[0-5]);
mysql> select ip from foo where ip regexp '^\\s*[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]\\s*';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM