regexp problem

K

Karuna

Hi
If I want to validate certain IP Address range
say in an IP Address range of : "15.70.186.15-100",
here where 15.70.186 is the network id and 15-100 is
the host id. I want to discover all IP Addresses that
fall in between 15.86.70.15 - 15.70.186.100 .
what regexp do i need to use ?

regds
kbs
 
A

Andy Shitov

If I want to validate certain IP Address range
say in an IP Address range of : "15.70.186.15-100",
here where 15.70.186 is the network id and 15-100 is
the host id. I want to discover all IP Addresses that
fall in between 15.86.70.15 - 15.70.186.100 .
what regexp do i need to use ?

Do you really need to use regular expression? If in future you will
change IP range you should rewrite regex. Regexes are not too sutaible
to deal with range checking (it is possible but the expression is not
human-friendly).

I think the best is to write a two-line function such as

sub check_ip_range{
my @ip = $_[0] =~ /^15\.70\.186\.(\d+)$/;
return ($ip[3] >= 15 && $ip[3] <= 100);
}
 
A

Andy Shitov

If I want to validate certain IP Address range
say in an IP Address range of : "15.70.186.15-100",
here where 15.70.186 is the network id and 15-100 is
the host id. I want to discover all IP Addresses that
fall in between 15.86.70.15 - 15.70.186.100 .
what regexp do i need to use ?

Do you really need to use regular expression? If in future you will
change IP range you should rewrite regex. Regexes are not too sutaible
to deal with range checking (it is possible but the expression is not
human-friendly).

I think the best is to write a two-line function such as
sub check_ip_range{
my ($ip) = $_[0] =~ /^15\.70\.186\.(\d+)$/;
return ($ip >= 15 && $ip <= 100) ? "+" : "-";
}

print check_ip_range ("15.70.186.90");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top