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

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.

Replies are listed 'Best First'.
Re: Re: Clueless newbie - help!
by dsb (Chaplain) on May 08, 2002 at 21:25 UTC
    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