需求:匹配公网地址。即除以下地址外,均为公网地址。
1、10.*.*.*
2、172.16~31.*.*
3、192.168.*.*
写三条即可,但最好写在一条正则式里。
1、10.*.*.*
2、172.16~31.*.*
3、192.168.*.*
写三条即可,但最好写在一条正则式里。
1
Automan Aug 21, 2014 '^(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.)'
|
2
joyoner OP |
3
jsonline Aug 21, 2014 via Android
不用正则也可以啊
|
4
xcv58 Aug 21, 2014 via Android
我觉得先用正则匹配出所有的 IP 地址,然后再判断是否是公网地址比较好。
|
5
qq529633582 Aug 21, 2014
匹配公网地址 != 排除私网地址
LZ列出的三个IP段是 http://tools.ietf.org/html/rfc1918#section-3 中的Private Address,还需要排除127.0.0.0/8等特殊IP(见 http://tools.ietf.org/html/rfc5735) |
6
yinheli Aug 21, 2014
|
7
yinheli Aug 21, 2014 sorry 地址有误, 是这个
https://gist.github.com/yinheli/92b918acd5b765377dbe |
8
zyxfsky Aug 22, 2014 ^\d{1,3}(?<!10)(?<!(172|192))\.\d{1,3}\.\d{1,3}\.\d{1,3}
好像越想越复杂了,复杂的断言写不来了,断言前面不是172后,同时断言后面不是16-31不知道怎么写,同等高手,求学习 |
11
ToughGuy Aug 22, 2014 ^(?!((127|10)\.\d{1,3}|192\.168|172\.16))\d{1,3}(\.\d{1,3}){3}
|
12
ToughGuy Aug 22, 2014
[root@test ~]# cat ip
115.239.211.110 127.0.0.1 192.168.1.1 10.10.16.1 172.16.1.1 199.71.213.22 [root@test ~]# grep -P '^(?!((127|10)\.\d{1,3}|192\.168|172\.16))\d{1,3}(\.\d{1,3}){3}' ip 115.239.211.110 199.71.213.22 |
13
zodiac1111 Aug 22, 2014
gist突兀有点吓人
|
15
zyxfsky Aug 22, 2014 刚又测试了下,看看这个能否满足你,我测试是ok的
^(?!10\.)\d{1,3}\.(?(?<=172\.)(?!(1[6-9]|2[0-9]|3[0-1])\.))(?(?<=192\.)(?!168\.))\d{1,3}(\.\d{1,3}){2}$ |