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