Solved

Need perl regex for ip range

Posted on 2009-04-09
8
960 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

861 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

28 Experts available now in Live!

Get 1:1 Help Now