Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Regular expression range question (was: Clueless newbie - help!)

by odosbucket (Initiate)
on May 08, 2002 at 20:58 UTC ( [id://165175]=perlquestion: print w/replies, xml ) Need Help??

odosbucket has asked for the wisdom of the Perl Monks concerning the following question:

I know zip about perl, but am modifying a script to generate some statistics. I'm trying to modify an if statement to include a range of ip addresses - the statement starts out looking like this:

if ($ip =~ /123\.45\.678\.<p>
How do I put in a range of numbers for the last segment of the ip address? The range needs to be 1-126 for one set and 17-31 for another. Nothing I've tried works, and my brief digging around on the web hasn't revealed an answer. Help?!? Thanks!

Edit kudra, 2002-05-07 Changed title; added code tags

Replies are listed 'Best First'.
Re: Clueless newbie - help!
by maverick (Curate) on May 08, 2002 at 21:17 UTC
    Checking to see if an ip address is within a certain range is doable with a regexp....but I don't think that's the best way to handle it as the regexp quickly becomes really nasty.

    Rather, check out a module called Net::IP::Match. From it's description it seems to do exactly what you need.

    HTH

    /\/\averick
    OmG! They killed tilly! You *bleep*!!

Re: Clueless newbie - help!
by runrig (Abbot) on May 08, 2002 at 21:24 UTC
      Thanks to Amel and all of you for the suggestions! It works now. Thank you thank you!!! --odosbucket
Re: Clueless newbie - help!
by JayBonci (Curate) on May 08, 2002 at 21:22 UTC
    A really general regexp that you might use looks like this:
    my $txt = "169.254.0.210"; if($txt =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.(\d{1,3})/ and ($1 < 250 && $1 > 220)) { print "VALID ADDRESS!\n"; }
    You might notice the pattern in the regexp that looks like \d{1,3}\. which means "match any number at least one time, and no more than three times and then a dot". This is a very general IP pattern. The above code also matches the last set (via the parenthases), and then compares it in the other half of the if statement.

    To apply it to your specific example:
    if($ip =~ /123\.45\.678\.(\d{1,3})/ and ($1 < 1 and $1 > 126)) #code here
    If you are SURE of the first three octects of your IP address, this will work just fine for you. Hope this helps.

        --jb

    Update In response to Amel's posting, yes, you'd have to test it for IP validity (and I wouldn't use it to match a whole IP in reality), but it matches the general pattern, and will work simply if all you need to test is the last octect. Zero is a valid IP digit if not in the first or last octect.
      This is more like a very general 3-digit match regex. Your regex will match numbers from 256 to 999 as well, which are outside of the valid IP range(0-255). Even 0 and 255 should not be used since those are reserved IP addresses.




      Amel
      This is my cool %SIG
Re: Clueless newbie - help!
by dsb (Chaplain) on May 08, 2002 at 21:23 UTC
    Um, if I'm clear, you need to know how to match a number from 1-126 or from 17-31:
    # tests for a 3 digit number from 1 - 126 $num =~ m/^(1(?:[01]\d|2[0-6])|\d?\d)$/; # tests for a 2 digit number from 17 - 31 $num =~ m/^(1[7-9]|2\d|3[01])$/;
    This is a bit messy so you may want to investigate methods by which you can check IP's that don't use a regex. Perhaps one of the Net modules.




    Amel
    This is my cool %SIG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://165175]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-19 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found