我写了这段代码,但它不起作用。 有人能指出问题吗?
sub match_ip()
{
my $ip = "The IP address is 216.108.225.236:60099";
if($ip =~ /(\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}\:\d{1-5})/)
{
print "$1\n";
}
}
编辑: 我只想提取IP地址,不进行任何验证。
我写了这段代码,但它不起作用。 有人能指出问题吗?
sub match_ip()
{
my $ip = "The IP address is 216.108.225.236:60099";
if($ip =~ /(\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}\:\d{1-5})/)
{
print "$1\n";
}
}
编辑: 我只想提取IP地址,不进行任何验证。
将{1-3}
更改为{1,3}
,同样的操作也适用于{1-5}
-> {1,5}
Regexp::Common
中的 Regexp::Common::net
可能会包含您所需的正则表达式。请注意保留HTML标记。或者,您可以使用Data::Validate::IP
,但需要注意的是它无法识别端口,因此您需要在:
上进行split
。
use strict;
use warnings;
use Data::Validate::IP;
my $ip_with_port="216.108.225.236:60099";
my $ip=(split /:/,$ip_with_port)[0];
my $validator=Data::Validate::IP->new;
if($validator->is_ipv4($ip))
{
print "Yep, $ip is a valid IPv4 address.\n";
}
else
{
print "Nope, $ip is not a valid IPv4 address.\n";
}
Yep, 216.108.225.236 is a valid IPv4 address.
请用逗号替换破折号。
/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5})/
while (my $ip = <DATA>) {
chomp $ip;
# older version
# if($ip =~ /(\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}\:\d{1-5})/)
# see below for explanation
if ($ip =~ /\b(\d{1,3}(?:\.\d{1,3}){3}:\d{1,5})\b/)
{
print "$ip - matches\n";
} else {
print "$ip - does not match\n";
}
}
__DATA__
216.108.225.236:60099
4.2.2.1:1
216.108.225.236:0
1216.1108.1225.1236:1234
216.108.225.236x:123
9216.108.225.236:8472
10.10.10.10
结果:
216.108.225.236:60099 - matches
4.2.2.1:1 - matches
216.108.225.236:0 - matches
1216.1108.1225.1236:1234 - does not match
216.108.225.236x:123 - does not match
9216.108.225.236:8472 - does not match
10.10.10.10 - does not match
/\b # word boundary
( # start memory capture group 1
\d{1,3} # one to three digits, first octat
(:? # start non memory capture group, notice ?:
\.\d{1,3} # a literal dot followed by an ip octet
) # end non memory capture group
{3} # three times of dots and ip octets
: # match a colon
\d{1,5} # port number, one to five digits
) # end of memory capture group 1
\b # word boundary
333.108.225.236:123
这样的数据是无效的 IP,但会通过您的检查。 - Will Sheppard一个较短版本的:
/((\d{1,3}\.){3}\d{1,3}\:\d{1,5})/
破折号“-”变成逗号“,”,即 {1,3}
和 {1,5}
,如其他答案已经指出的那样。
我的改变是将 \d{1,3}\.\d{1,3}\.\d{1,3}\.
组合成一个,即 (\d{1,3}\.)
并使用 {3}
精确匹配 3 次。
#!/usr/bin/perl
$str = 'IP address is : 70.21.311.105';
if ($str =~ m/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) {
if ($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ) {
print "Valid $str\n";
} else {
print "invalid IP $str\n";
}
}
__END__
if
条件,可以与第一个条件一起写成这样 if($str =~ m/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/ && ($1<=255 && $2<=255 && $3<=255 && $4<=255 ))
。 - serenesatmy $ip = "195.249.61.14";
my @ips = (
"set protocols bgp group IBGP-RRCL-CUSTOMER neighbor 195.249.61.142",
"set protocols bgp group IBGP-RRCL-CUSTOMER neighbor 195.249.61.14",
"set protocols bgp group IBGP-RRCL-CUSTOMER neighbor 195.249.61.141"
);
foreach (@ips) {
print "$_\n" if ( /\b$ip\b/ );
}
输出:
set protocols bgp group IBGP-RRCL-CUSTOMER neighbor 195.249.61.14
试一下:
$variablename=~m/((((0-9)|((1-9)(0-9))|(1([0-9]){2})|(2[0-4][0-9])|(2[5][0-5]))\.){3})((0-9)|((1-9)(0-9))|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))/)
use strict;
use warnings;
open(FH,"<fileName.txt") or die "file not found ,$_";
while(my $line=<FH>)
{
push(my @arr,($line));
foreach my $arrVal (@arr)
{
if($arrVal=~/IPv4 Address(?=.*\b((25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2 [0-4]\d|[0-1]?\d?\d)){3})\b)/)
{
print "$arrVal\n";
}
}