Michael -- ...and then Michael Norris said... % % This subroutine is supposed to chech the validity of an IP address. 4 numbers (1 - 255) separated by ".". But my regular expression doesn't seem to be working out for me. % ... % if ($_[0] =~ m/([1-2][0..5]*[0..5]*)\.\1\.\1\.\\s*$/) {
I don't know the best answer to your question, but I do know that the expression you've built doesn't limit to 1-255. Your rule says one 1 or one 2 any number of 0-5 any number of 0-5 and so "1" and "155" work, but "186" doesn't work even though it should and "1555555" works even though it shouldn't. Furthermore, I *think* that by matching \1 repeatedly you've then said that only a.a.a.a or b.b.b.b (where a or b matches the expression) works, rather than a.b.c.d (you want to match the same expression, but I believe the \1 means the same *result*). It seems to me easiest split this into four pieces and check the value of each, so something like ($p1,$p2,$p3,$p4) = split(/\./, $_[0]) foreach $p ($p1 $p2 $p3 $p4) { ( $p > 0 ) && ( $p < 256 ) || die "whoa -- $p is not in range!"; } would do the trick. You *might* want to ensure that only three dots, and no non-numbers, are fed in, in which case you could first m/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ || die "only numbers and three dots!" and then move on to checking the values. YMMV. All of this code is untested and off the top of my newbie head. The two tests could probably be combined into one, even, as well as everything written more elegantly. But I *do* know that you'll need to start with a regexp that DWYM,NWYS (does what you mean, not what you say :-) HTH & HAND :-D -- David T-G * It's easier to fight for one's principles (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie (work) [EMAIL PROTECTED] http://www.justpickone.org/davidtg/ Shpx gur Pbzzhavpngvbaf Qrprapl Npg!
msg25475/pgp00000.pgp
Description: PGP signature