Solved

Need perl regex for ip range

Posted on 2009-04-09
8
972 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

728 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