On Jun 5, 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*$/) {

[0..5] means [05.] -- that is, any character of '0', '5', or '.'.
[0..5]* means zero or more of [0..5].
\1 means "match EXPLICITLY that text matched in the first () set".

You don't want ANY of those.  Not to mention, your regex, if it worked,
would deny an IP address 193, since 9 is not in [0-5].

Here's a working regex:

  my $num = qr{
      0?\d?\d  # match 0-9, 10-99, with optional leading 0's
    | 1\d\d    # match 100-199
    | 2[0-4]\d # match 200-249
    | 25[0-5]  # match 250-255
  }x;

  if ($ip_addr =~ /\b($num\.$num\.$num\.$num)\b/) {
    print "IP: $1\n";
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to