Rob Dixon wrote:
On 29/11/2010 23:46, John W. Krahn wrote:
As Rob said [2..254] is a character class that matches one character (so
"127.0.0.230" should match also.) You also don't anchor the pattern so
something like '765127.0.0.273646' would match as well. What you need is
something like this:
#!/usr/bin/perl
use strict;
use warnings;
my $ip = '127.0.0.255';
my $IP_match = qr{
\A # anchor at beginning of string
127\.0\.0\. # match the literal characters
(?:
[2-9] # match one digit numbers 2 - 9
| # OR
[0-9][0-9] # match any two digit number
This regex solution allows the IP address 127.0.0.01, which is out of
range.
Yes, sorry, that should be:
[1-9][0-9]
| # OR
1[0-9][0-9] # match 100 - 199
| # OR
2[0-4][0-9] # match 200 - 249
| # OR
25[0-4] # match 250 - 254
)
\z # anchor at end of string
}x;
if ( $ip =~ $IP_match ) {
print "IP Matched!\n";;
}
else {
print "No Match!\n";
}
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/