/ Published in: Perl

This code will validate not only the four octets contain between 1 and 3 numbers each, but also that the number they contain is between 0 and 255.
In all, there are 3 different regex's that I tried and all seem to work fine. The three regex's are:
1. m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/
2. m/\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?/
3. m/\d{1,2}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
The first one is used below. Enjoy!!!
In all, there are 3 different regex's that I tried and all seem to work fine. The three regex's are:
1. m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/
2. m/\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?/
3. m/\d{1,2}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
The first one is used below. Enjoy!!!
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/perl use strict; use warnings; my $ipaddr = <STDIN>; if( $ipaddr =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/ ) { if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255) { } else { } } else { }
Comments
