Verify if an IPv4 IP address is valid

/ Published in: Perl
Save to your folder(s)

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!!!

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.