Solved

Need perl regex for ip range

Posted on 2009-04-09
8
958 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
Comment Utility
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
Comment Utility
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
Comment Utility
^96\.[67](\.(1?\d?\d|2([0-4]\d|5[0-5]))){2}$
0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 100 total points
Comment Utility
>>  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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now