Solved

Need perl regex for ip range

Posted on 2009-04-09
8
971 Views
Last Modified: 2012-06-27
I need to have a perl regex expression to match the ip range 96.6.0.0 to 96.7.255.255
0
Comment
Question by:Melaleuca
8 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 24108452
I don't think you'll be able to do it with just a regex, as these are used to check strings.  Can you use any logic other than a regex?
0
 
LVL 5

Author Comment

by:Melaleuca
ID: 24108638
I have searched the web and have found ways to match for a IP address with a Regex expression. I'm am not that great with regex and don't fully understand how the string should look. It has to be a regex because that is all websense will allow.
0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 24108825
^96\.[67](\.(1?\d?\d|2([0-4]\d|5[0-5]))){2}$
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 100 total points
ID: 24113196
>>  I'm am not that great with regex and don't fully understand how the string should look

no need regular expression if you are not familiar. have you forgotten normal string comparison? sure, its lengthier than short regex, but at one look, you know what's it doing.

my $ip = "96.8.256.1";
my @list = split /\./,$ip;
if (  $list[0] == "96" && 
      ( $list[1] >= 6 && $list[1] <= 7) &&
      ( $list[2] >= 1 && $list[2] <=255) &&  
      ( $list[3] >= 1 && $list[3] <=255)    ) {
    print "ok";
}else{
    print "ip not ok";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24113510
did you want to accept 96.6.0.0 and 96.7.0.0  ?

I also note that the split and compare routine would accept things like
$ip = '96.6e0.1e1.22222e-3'
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24113911
>>I also note that the split and compare routine would accept things like
$ip = '96.6e0.1e1.22222e-3'

if that's the case, performing a check to see if its a number(integer) before doing the comparison should do it.

0
 
LVL 84

Expert Comment

by:ozo
ID: 24114069
6e0 and 1e1 are integers
so is '   1   '
But that may support your point that at one look, you know what's it doing.
At one look, it may not be quite as easy to know whether or not the regex would accept any of those.
And if one wants to change the ip range 96.6.0.0 to 96.7.255.255
into an  ip range 96.6.1.1 to 96.7.255.255
it may be less obvious at one look how to adjust the regex
but Melaleuca also said that It has to be a regex because that is all websense will allow.

Another way with comparisons:
 $v = eval ($ip = "96.8.256.1");
if( ($v & 255.255.255.255) eq $v && 96.6.0.0 le $v && $v le 96.7.255.255 ){ print "ok"; }

But this can be dangerous if you have
$ip='system"logout"'; #or worse
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24114147
i do not know about websense and why it can only use regex, but if that's the case, so be it.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question