I think this is why $Bill said use the syscall rather than the RE - IPs are also legally expressed as the raw 32 bit number in decimal, or as a subset of the dotted quad only including the elements required to dis-ambiguate depending on the subnet mask (with a subnet mask of 255.0.0.0, 127. is a valid IP representing the network (or is it broadcast?) address...)

not following standards will always bite you in the behind at some stage.. :)

On Tue, 13 Jan 2004 20:18:58 -0800, Glenn Linderman <[EMAIL PROTECTED]> wrote:

On approximately 1/13/2004 7:01 PM, came the following characters from
the keyboard of Jamie Murray:
Hi Glenn,
I have worked on this further and looked at some of the previous posts.
I have tried this with all different combinations of ip address and this has
worked.
Of course I got the idea from a previous post from Alex and Mark Thomas.
Please understand I could have just copied and pasted Mark's solution but
then I wouldn't have learned anything.

if($num =~ /^(([0-1]{0,1}[0-9]{0,1}[0-9]|2[0-4][0-9]|25[0-5])\.?){4}$/)

Well, this REGEX appears like it will match all the desired cases. However, the optional \. and the {4} combine to allow it to successfully match some things that you might not want to match. Mark's post is better, but let us learn why.


I've mentioned this as a difference before, but the use of \d instead of [0-9] and ? instead of {0,1} both tend to aid readability, because they are shorter, so the specific concept that they represent, while in one sense "just a shorthand" for exactly the way you express it, becomes more readable to the people that understand those shortcuts, because it takes less reading and thought to grasp the concept. When you see [0-9], all 5 characters have to be examined to realize that the expression means "a numeric character", whereas \d requires only the examination of 2 characters to arrive at the same conclusion. Similarly for ? vs {0,1}. These shortcuts can save time in reading the expression, and also stand out as different from [0-4] and {0,4}, and are for very common cases where the benefits of knowing and understanding the shortcuts help significantly in quickly understanding the REGEX.

Now the \.?){4} vs spelling out 4 instances. I really don't know anything about the edge cases that people talk about that have fewer than 3 dots, and/or more than 3 digit groupings of numbers.... all the IP addresses I have ever seen have been so called "dotted quads". I'm sure there is an RFC somewhere that describes the exact details of what is and is not legal IP address notation. Clearly, an IP address is "just a 32-bit number" and can be expressed in a number of ways other than the "dotted quad". However, the Cookbook expression that Alex started with was clearly intended to match "dotted quads" and only "dotted quads", and so I think is the goal you are trying to achieve.

Your REGEX allows fewer than 3 dots. The \.? is optional in each of the 4 groupings, not only for the last one. So your REGEX would permit things that aren't "dotted quads", such as

    127.999
    9999
    1.2.34
    123234135157

When testing software of any sort, it is important to be sure that it accepts the things you want to accept, and also rejects the things it doesn't understand.

I feel that Mark's post is the better choice though and much
slicker than mine.

my $octet = qr/\d|[01]?\d\d|2[0-4]\d|25[0-5]/;
my $valid_ip = qr/^$octet\.$octet\.$octet\.$octet$/o;

print "yes" if $ip =~ $valid_ip;

Yes, Mark's solution is better than yours, in that it only accepts dotted quads, and it is better than the Cookbook solution (in my opinion) because it removes the complex redundancy, and can be expressed in half the lines even with the enhanced clarity.


Happy learning.


----- Original Message ----- From: "Glenn Linderman" <[EMAIL PROTECTED]>
To: "Jamie Murray" <[EMAIL PROTECTED]>
Cc: "$Bill Luebkert" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Tuesday, January 13, 2004 6:24 PM
Subject: Re: REGEX help!



On approximately 1/13/2004 6:49 AM, came the following characters from
the keyboard of Jamie Murray:

Hi guys,
I have seen my error which I have overlooked and don't mind admitting

it.


: )  Course don't hold it against me cause I'm just eager to learn
and try things out.
My regex works it matches exactly what I want but not all possibilities

.


I overlooked the simple fact that alex wants not 0 or 2 or 5 or 4 but

254


or less.
Course with the example I posted Alex can easily adjust for this.

So my method excludes 65 and up ,165 and up but not 254 to 200 or 154 to

100


or 54 or less.
So yes Bill im excluding 192 amongst others in my regex I see your

point.


Ok so this gets a little deeper than expected because I can have 199 but

not


299

/^([0-1]{0,1}[0-9][0-9] | 2[0-4][0-9] | 25[0-5])\. ([0-1]{0,1}[0-9][0-9]

|


2[0-4][0-9] | 25[0-5])\. ([0-1]{0,1}[0-9][0-9] | 2[0-4][0-9] |

25[0-5])\.


([0-1]{0,1}[0-9][0-9] | 2[0-4][0-9] | 25[0-5]) $/

so now we are checking for 000 or 00 to 199 or 200 to 249 or 250 to 255
followed by \.
Now I should have this right. Making mistakes sure helps you learn and
think things through more thoroughly.

How is that or do you have anymore suggestions.

As someone else pointed out, you are rapidly approaching the REGEX given in the Perl Cookbook. Once you add a case to handle single digit numbers you will be there.

The only other differences are that you are using {0,1} which is exactly
the same as ?, and you are using [0-9] which is exactly the same and \d.
 In both cases, the latter of the two equivalent expressions is shorter
to express, and used by the REGEX in the Perl Cookbook.



----- Original Message ----- From: "Jamie Murray" <[EMAIL PROTECTED]> To: "$Bill Luebkert" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Tuesday, January 13, 2004 9:40 AM Subject: Re: REGEX help!




BiLL,
If you check my second post I made the correction but I'm still correct

in


my example and method.
Actually the e-mail from Raul Davletshin pretty much verifys what I had
also stated and he's also correct.
As for explaining [0-2]  0 or 1 or 2 are all possibilities of course but
only one(unless using ? but thats another story)
so wheres the problem your explaining something we already know.
Also the example I gave Alex can be adjusted to his needs using class []
and range {}. At least he will know how to put together
some type of expression that works instead of just relying on built in
functions.
As for your post down below you can check numbers that way.
Did you run this in a script before you decided it doesn't work because

it


worked perfectly for me.
Please run what I have below and correct what is "actually incorrect"

not


what you "think is incorrect"
I'm all for learning and am just trying my best.

Thanks!


----- Original Message ----- From: "$Bill Luebkert" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, January 13, 2004 5:07 AM Subject: Re: REGEX help!




Jamie Murray wrote:



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]$/)



   ^^^  ^^^  ^^^
The digits can be 0-9, not 0-2, 0-4 or 0-5.  eg: 192.168.0.1 is a legal

IP



You can't check a number range this way.



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"


-- ,-/- __ _ _ $Bill Luebkert

Mailto:[EMAIL PROTECTED]



(_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
/ ) /--<  o // //      Castle of Medieval Myth & Magic

http://www.todbe.com/



-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers

stuff)


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

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



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



-- 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