我一直在阅读有关Regex to ONLY match PUBLIC IPv4 address的文章,并尝试了所有给出的解决方案,但实际上没有一个能够准确地匹配公共IP地址。
示例IP
[user@linux ~]$ cat ip.txt
1.1.1.1
8.8.8.8
10.1.1.1
127.0.0.1
[user@linux ~]$
解决方案1- https://stackoverflow.com/a/39195704/11392987
[user@linux ~]$ egrep '^([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?<!172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31))(?<!127)(?<!^
10)(?<!^0)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?<!192\.168)(?<!172\.(16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31))\.([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])(?<!\.255$)$' ip.txt
[user@linux ~]$
Sol 2- https://stackoverflow.com/a/33453740/11392987
[user@linux ~]$ egrep '(\d+)(?<!10)\.(\d+)(?<!192\.168)(?<!172\.(1[6-9]|2\d|3[0-1]))\.(\d+)\.(\d+)' ip.txt
[user@linux ~]$
Sol 3- https://stackoverflow.com/a/46399203/11392987
[user@linux ~]$ egrep '(^0\.)|(^10\.)|(^100\.6[4-9]\.)|(^100\.[7-9]\d\.)|(^100\.1[0-1]\d\.)|(^100\.12[0-7]\.)|(^127\.)|(^169\.254\.)|(^172\.1[6-
9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.0\.0\.)|(^192\.0\.2\.)|(^192\.88\.99\.)|(^192\.168\.)|(^198\.1[8-9]\.)|(^198\.51\.100\.)|(^203.0\
.113\.)|(^22[4-9]\.)|(^23[0-9]\.)|(^24[0-9]\.)|(^25[0-5]\.)' ip.txt
10.1.1.1
127.0.0.1
[user@linux ~]$
10.1.1.1 & 127.0.0.1
是专用IP地址,而不是公网IP
Sol 4- https://stackoverflow.com/a/57077560/11392987
[user@linux ~]$ egrep '^(?!^0\.)(?!^10\.)(?!^100\.6[4-9]\.)(?!^100\.[7-9]\d\.)(?!^100\.1[0-1]\d\.)(?!^100\.12[0-7]\.)(?!^127\.)(?!^169\.254\.)(?
!^172\.1[6-9]\.)(?!^172\.2[0-9]\.)(?!^172\.3[0-1]\.)(?!^192\.0\.0\.)(?!^192\.0\.2\.)(?!^192\.88\.99\.)(?!^192\.168\.)(?!^198\.1[8-9]\.)(?!^198\.
51\.100\.)(?!^203.0\.113\.)(?!^22[4-9]\.)(?!^23[0-9]\.)(?!^24[0-9]\.)(?!^25[0-5]\.)(([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])\.([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]))$' ip
.txt
[user@linux ~]$
Sol 5- https://www.bigdatamark.com/regexp-for-extracting-public-ip-address/
[user@linux ~]$ egrep '\b(?!(10)|192\.168|172\.(2[0-9]|1[6-9]|3[0-2]))[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' ip.txt
[user@linux ~]$
发布于 2019-11-12 20:28:16
试试this正则表达式。匹配所有公网It,不匹配预留It。
编辑-尝试使用grep -P
而不是egrep
,因为egrep不支持lookahead。
grep -P '^(?!^0\.)(?!^10\.)(?!^100\.6[4-9]\.)(?!^100\.[7-9]\d\.)(?!^100\.1[0-1]\d\.)(?!^100\.12[0-7]\.)(?!^127\.)(?!^169\.254\.)(?!^172\.1[6-9]\.)(?!^172\.2[0-9]\.)(?!^172\.3[0-1]\.)(?!^192\.0\.0\.)(?!^192\.0\.2\.)(?!^192\.88\.99\.)(?!^192\.168\.)(?!^198\.1[8-9]\.)(?!^198\.51\.100\.)(?!^203.0\.113\.)(?!^22[4-9]\.)(?!^23[0-9]\.)(?!^24[0-9]\.)(?!^25[0-5]\.)(([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])\.([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]))$' ip.txt
输出
1.1.1.1
8.8.8.8
发布于 2019-11-12 23:58:33
真正的问题是为什么它必须是一个单一的正则表达式?
如果输入经过验证,则可以实现更简单的正则表达式,通过验证,我的意思是输入中没有越界的ip地址。如果是这样的话,像这样的东西可以工作。
grep "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}" ip.txt |
grep -v "10\(\.[0-9]\{1,3\}\)\{3\}\|192\.168\(\.[0-9]\{1,3\}\)\|172\.\(1[6-9]\|2[0-9]\|3[01]\)\.[0-9]\{1,3\}\.[0-9]\{1,3\}\|127.0.0.1"
如果需要验证,那么正则表达式就会变得更加复杂
grep "\([0-9]\.\|[1]\{,1\}[0-9]\{2\}\.\|2[0-4][0-9]\.\|25[0-5]\.\)\{3\}\([0-9]\|[1]\{,1\}[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)" ip.txt |
grep -v "10\(\.[0-9]\|\.[1]\{,1\}[0-9]\{2\}\|\.2[0-4][0-9]\|\.25[0-5]\)\{3\}" |
grep -v "172\.\(1[6-9]\|2[0-9]\|3[01]\)\.\([0-9]\.\|[1]\{,1\}[0-9]\{2\}\.\|2[0-4][0-9]\.\|25[0-5]\.\)\([0-9]\|[1]\{,1\}[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)" |
grep -v "192.168\(\.[0-9]\|\.[1]\{,1\}[0-9]\{2\}\|\.2[0-4][0-9]\|\.25[0-5]\)\{2\}" |
grep -v "127.0.0.1"
为了简单起见,我将私有ip地址空间划分为3类,并通过管道将它们连接在一起。
\|
并且只有两个grep命令。
https://stackoverflow.com/questions/58818048
复制相似问题