得分
36
Advertisement
答案 (12)
解决方案找到了!
Advertisement
得分
10
2010-10-24 13:50:10 +0000
这个怎么样? -
perl -MRegexp::Common=net -ne '/($RE{net}{IPv4})/ and print "$1\n"' /etc/hosts
```0x1
0x1&
Joe Casadonte
http://superuser.stackexchange.com/users/51175
Advertisement
得分
5
2012-06-26 11:15:04 +0000
-w / --word-regexp
标志为grep,使得它只能在字的边界上进行匹配,这意味着你的匹配必须被空格包围或者在行的开始/结束处开始/结束!
Dominik George
http://superuser.stackexchange.com/users/51175
Advertisement
得分
5
2012-09-13 08:35:57 +0000
要只找4个八位数的匹配(不包括1.1.1.1.1.1等),可以用这个方法。
grep -P '(?<=[^0-9.]|^)[1-9][0-9]{0,2}(\.([0-9]{0,3})){3}(?=[^0-9.]|$)'
它应该不会检测到非IP地址. 表达式可以更复杂,以验证更多的东西,但这应该适用于大多数情况。它不会匹配前面的0,因为010.1.12.1不是写IP地址的常用方式。
Stefan Seidel
http://superuser.stackexchange.com/users/51175
Advertisement
Advertisement
得分
0
2015-04-17 11:17:29 +0000
在TCL中匹配一个ip地址的正则表达式
设置一个 “192.168.10.25”
if {[regexp {^(([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])$} $a]} { puts "yes" } ```。
abhilash.malla
http://superuser.stackexchange.com/users/51175
得分
0
2015-10-29 18:58:11 +0000
我使用egrep "^([0-9]{1,3}\.){3}[0-9]{1,3}" /etc/hosts
来匹配行首的IP地址。它也可以不使用^来在IP地址前留出空白或其他字符。
[0-9]{1,3} --> this matches a number between 1 and 999.
\. --> this is to add the dot.
([0-9]{1,3}\.){3} --> get a number with a dot 3 times.
[0-9]{1,3} --> finally add the fourth number.
得分
0
2016-09-01 09:46:09 +0000
grep -Eo ‘([0-9]{1,3}.?){4}’
例子 : curl http://korben.info/ip | grep “IP visible depuis mon serveur” | grep -Eo ‘([0-9]{1,3}.?){4}’
user2357585
http://superuser.stackexchange.com/users/51175