?
Solved

Need perl regex for ip range

Posted on 2009-04-09
8
Medium Priority
?
973 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 1200 total points
ID: 24108825
^96\.[67](\.(1?\d?\d|2([0-4]\d|5[0-5]))){2}$
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Suggested Courses

800 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