On approximately 1/12/2004 8:36 PM, came the following characters from
the keyboard of Jamie Murray:
Hey Alex,
I jumped a little quick there, the previous post does work but I had a doh
moment and forgot your upper range match could only be 254 at most.
Sorry about that.

if($num =~
/^[0-2][0-5][0-4]\.[0-2][0-5][0-4]\.[0-2][0-5][0-4]\.[0-2][0-5][0-4]$/)
 after each class [] use {num,num} to adjust for a part of the ip not having
a number.

so for example

if($num =~
/^[0-2]{0,1}[0-5][0-4]\.[0-2][0-5][0-4]\.[0-2][0-5][0-4]\.[0-2][0-5][0-4]$/)
matches ip's like these
 "three digit 254 or less"."three digit 254 or less"."three digit 254 or
less"."three digit 254 or less".
or
"two digit 54 or less"."three digit 254 or less"."three digit 254 or
less"."three digit 254 or less"

Oh please. Think about some test cases.


Do you think that Alex would want to match 192.168.10.10 ? I think so, but your regex wouldn't.

See $Bill's reply, or the original Cookbook expression, for a much better suggestion.

But to get back to one of the questions that Alex originally asked:

alex p wrote:
I have been trying to find a regex to validate IP ranges and found the
following:

m{
^ ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
\. ( \d | [01]?\d\d | 2[0-4]\d | 25[0-5] )
$ # from the Perl Cookbook, Recipe 6.23,
}xo # pages 218-9, as fixed in the 01/00 reprint


can someone explain this REGEX to me

The first thing one observes by the structure of the above REGEX is that it consists of 4 repeated identical expressions (one on each line). The first one starts at the beginning of the string (^), the last one ends at the end of the string ($), and they are separated by periods (\.).


Now let's examine one of the repeated groups. Each group has 4 alternatives, each separated by |.

Alternative 1 is any single digit. This covers the range of values from 0-9 inclusive.

Alternative 2 is any pair of digits, or optionally any pair of digits preceeded by either 0 or 1. This covers the range of values from 0-199 inclusive. Note that I did NOT say it covers the range of values from 10-199... the values from 0-9 can also legally be expressed as 01 - 09, or 001 - 009.

Alternative 3 is a 2, followed by a digit from 0-4, followed by any digit. This covers the range of values: 200-209, 210-219, 220-229, 230-239, and 240-249, the union of which is 200-249.

Alternative 4 is 25, followed by a digit from 0-5, so it covers the range of values from 250-255.

Put together, the alternatives cover the complete range of 1 to 3 digit numbers ranging in value from 0-255.

The whole expression covers the complete range of 1 to 3 digit numbers ranging in value from 0-255, grouped in 4 groups separated by a single period character. This is the usual form for expressing IP addresses.

--
Glenn -- http://nevcal.com/
===========================
The best part about procrastination is that you are never bored,
because you have all kinds of things that you should be doing.

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to