As selective, less verbose, non-repetitive, more readable, better IMHO:
my $octet_dec = qr{ [01]?\d\d? | 2[0-4]\d | 25[0-5] }xms;
my $ipv4_dec = qr{ $octet_dec (?: [.] $octet_dec){3} }xms;
Update: Or better yet, as already mentioned, Regexp::Common::net.
Give a man a fish: <%-(-(-(-<
| [reply] [Watch: Dir/Any] [d/l] [select] |
Hi Discipulus,
your suggestion is a pure regex, which makes perfect sense in J. Friedl's book, but I do not think it is more selective than my proposal, mixing a regex and some arithmetics, which looks for four dot-separated integer numbers smaller than 256. (Except that I used \d instead of [0-9] for brevity, so that my regex might match (non-Arabic) Unicode digits, but that's easily fixed.) | [reply] [Watch: Dir/Any] [d/l] [select] |
perl -ne 'print if /(\d{1,3}\.){3}(\d{1,3})/ and $1 < 256 and ... and $4 < 256;'
Unfortunately, capture groups don't change their numbering under a counted quantifier (a misapprehension I've suffered more than once), so it's necessary to use four explicit captures for the above to work:
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le
"$_ = '12.34.56.78';
;;
/(\d{1,3}\.){3}(\d{1,3})/;
dd [ $1, $2, $3, $4 ];
;;
/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
dd [ $1, $2, $3, $4 ];
"
["56.", 78, undef, undef]
[12, 34, 56, 78]
Give a man a fish: <%-(-(-(-<
| [reply] [Watch: Dir/Any] [d/l] [select] |
$_ = '192.168.2.111';
my @l;
/^(?:([01]?\d\d?|2[0-4]\d|25[0-5])(?{push@l,$1})\.){3}([01]?\d\d?|2[0-
+4]\d|25[0-5])$/;
print join('-',@l,$2),"\n";
__END__
192-168-2-111
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [Watch: Dir/Any] [d/l] |
| [reply] [Watch: Dir/Any] |