139

我一直在尝试为 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 数字,或者对我不起作用。

4

43 回答 43

118

你已经得到了一个可行的答案,但如果你好奇你原来的方法出了什么问题,答案是你需要在你的交替周围加上括号,否则(\.|$)只有当数字小于 200 时才需要。

'\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b'
    ^                                    ^
于 2011-03-12T18:08:06.197 回答
104
^((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

于 2014-09-22T07:46:17.580 回答
92

最新、最短、最不可读的版本(49 个字符

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

这些[0-9]块可以\d在 2 个地方替换 - 使其可读性降低,但绝对更短。

甚至更新、更短、可读性第二差的版本(55 个字符

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

此版本查找 250-5 案例,然后巧妙地对案例的所有可能案例进行 OR 运算200-249 100-199 10-99。请注意,这|)部分不是错误,但实际上是对 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 可能以 a 结尾的情况.

替代答案,使用一些较新的技术(71 个字符

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

在不支持前瞻的正则表达式实现中很有用

最早的答案(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.看起来像这里大多数其他答案那样的东西,并且需要额外的正则表达式来拒绝与那个类似的情况 - 即0起始数字和以 a 结尾的 ip.

于 2016-04-21T05:01:16.967 回答
14

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 库

编辑:这(\.|$)部分看起来很奇怪

于 2011-03-12T17:33:27.277 回答
9

我认为许多阅读这篇文章的人会寻找更简单的正则表达式,即使它们匹配一些技术上无效的 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}/

我对所有这些的测试都在这里在线。

于 2018-07-02T09:00:59.123 回答
8

我正在为 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)

以防万一其他人正在寻找验证“不包括公共私有地址的互联网 IP 地址”

于 2015-11-09T21:28:26.837 回答
6

这是一个更好的附加了通过/失败的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
于 2021-06-23T16:58:20.873 回答
5

上面的答案是有效的,但是如果 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
于 2018-11-23T07:32:04.640 回答
4

这比一些长一点,但这是我用来匹配 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])$
于 2017-12-24T09:13:28.417 回答
4

''' 这段代码对我有用,就这么简单。

在这里,我采用了 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”,依此类推。'''

于 2019-03-17T08:58:45.353 回答
3
(((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如果第二个数字是0 through ,则任何以 开头的三位数字4

  • 25如果第三个数字是0 through ,则任何以 开头的三位数字5

(((25[0-5])|(2[0-4]\d)|(1\d{2})|(\d{1,2}))\.)让我们从一组四个嵌套子表达式开始,我们将按相反的顺序查看它们。(\d{1,2})匹配任何一位或两位数的数字或0通过99. 匹配以(开头的(1\d{2})任何三位数字,后跟任意两位数字),或通过. 的数字。通过匹配数字。通过匹配数字。这些子表达式中的每一个都包含在另一个子表达式中,每个子表达式之间都有一个(因此四个子表达式中的一个必须匹配,而不是全部匹配)。在数字范围匹配之后,然后是整个系列(所有数字选项加上11100199(2[0-4]\d)200249(25[0-5])250255|\..\.) 包含在另一个子表达式中,并使用 . 重复三次{3}。最后,重复数字范围(这次没有尾随\.)以匹配最终的 IP 地址编号。通过将四个数字中的每一个限制为 和 之间的值0255此模式确实可以匹配有效 IP 地址并拒绝无效地址。

摘自:本·福塔。“学习正则表达式。”</a>


^如果 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

于 2018-06-01T20:19:09.640 回答
3
/^(?:(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?1)$/m
于 2019-06-10T11:58:28.580 回答
3

对于从 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

对于 IPv4 CIDR 列表,"10.0.0.0/16", "192.168.1.1/32"我使用这个正则表达式:

^("(([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”

也许它可能会变短,但对我来说,我很容易理解。

希望能帮助到你!

于 2019-08-16T08:26:52.990 回答
2

我设法从所有其他答案构建了一个正则表达式。

(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}
于 2015-10-12T11:52:02.080 回答
2

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
于 2017-12-24T09:34:14.887 回答
1

带子网掩码:

^$|([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]))?)$
于 2018-04-05T15:33:51.400 回答
1

我试图让它更简单、更短。

^(([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

于 2019-12-26T04:45:31.150 回答
1

我试图让它更简单、更短。

^(([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

于 2019-12-26T04:49:36.570 回答
1

在文本中找到一个有效的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 正则表达式有少数误报匹配:

https://regexr.com/69dk7

用于 ip 地址的大部分正确匹配的正则表达式

仅使用正则表达式从文本文件中查找或提取有效 IP 地址是不可能的。如果不检查其他条件,您总是会得到误报匹配。

解决方案


我写了一个 liner perl从文本文件中提取 IP 地址。它有这样的条件:

  • 当ip地址在行首时,下一个字符为一个或多个空格字符(空格、制表符、换行...)
  • 当 ip 地址在行尾时,新行是下一个字符并且在 ip 地址之前是一个或多个空白字符
  • 在文本中间 - ip 地址之前和之后是一个或多个空白字符

结果是 perl 不匹配字符串 likehttps://84.25.74.125和另一个 URI 字符串。或者 ip 地址在行尾,点 char 在最后。但它会在文本中找到任何有效的 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 地址,我有一条消息:尝试自己找到解决方案!!!

于 2021-11-12T12:23:10.597 回答
0
    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 地址,其中 Linux 中的 009 被认为是八进制值,因此该地址不是您想的那个地址。我将该正则表达式更改如下:

    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();
        }
    }
于 2014-08-10T08:27:42.297 回答
0
-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,仅此而已。

于 2016-02-21T11:14:01.923 回答
0

我觉得这个是最短的。

^(([01]?\d\d?|2[0-4]\d|25[0-5]).){3}([01]?\d\d?|2[0-4]\d|25[0-5])$
于 2016-09-09T08:38:40.030 回答
0

我发现这个示例非常有用,而且它允许使用不同的 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
于 2017-03-09T10:58:20.467 回答
0
((\.|^)(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

于 2017-07-20T13:40:15.277 回答
0

只要 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
于 2017-09-13T22:19:12.190 回答
0

^((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


您可以想象上述所有类型的地址

于 2018-04-12T15:20:39.667 回答
0

我会使用 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]
于 2018-06-09T11:48:07.753 回答
0

我能想象的最精确、直接和紧凑的 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)$

但是......的性能/效率呢?对不起,我不知道,谁在乎?

于 2018-06-09T14:48:58.260 回答
0

试试这个:

\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
于 2018-07-18T08:56:15.123 回答
0
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
于 2018-09-03T16:15:27.630 回答
0

以下是验证 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]?)$
于 2018-12-12T21:20:30.240 回答
0

简单的方法

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

演示

于 2018-12-18T14:15:17.160 回答
0

这一个只匹配有效的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
于 2019-05-23T19:42:08.103 回答
0

我在这个页面中看到了非常糟糕的正则表达式..所以我自己来了:

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

在这里你可以验证它是如何工作

于 2019-08-08T23:46:08.393 回答
0

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])$
于 2020-01-25T13:27:40.933 回答
0

我的 [扩展] 方法 → 用于以空格分隔的 IP 地址的正则表达式:

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

使用 PCRE 前瞻。

于 2020-10-19T13:27:48.320 回答
0

试试这个

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

于 2021-05-30T06:56:53.953 回答
0

此模式排除 52 个符号并接受以零开头的 cucks。

/^(?:(?:[01]?\d?\d|2[0-4]\d|25[0-5])(?:\.|$)){4}\b$/
于 2021-06-09T22:00:42.077 回答
0

我的解决方案在这里:

^([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
于 2021-10-08T19:38:29.823 回答
0

要验证有效范围 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])
于 2021-10-09T01:44:05.297 回答
-1

这是正则表达式对我有用:
"\<((([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]))\>"

于 2016-10-16T18:17:49.203 回答
-3
mysql> select ip from foo where ip regexp '^\\s*[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]\\s*';
于 2012-01-23T18:15:40.990 回答
-3
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]);
于 2017-01-03T02:10:04.960 回答