/ 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:
- m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)/
- m/\d\d?\d?.\d\d?\d?.\d\d?\d?.\d\d?\d?/
- m/\d{1,2}.\d{1,3}.\d{1,3}.\d{1,3}/
The first one is used below. Enjoy!!!
Expand |
Embed | Plain Text
#!/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
Subscribe to comments
You need to login to post a comment.

That's surely true for a string like ' 172.168.0.1' ( not in valid format ) but '172.168.0.1 ' will be in valid format. You'll have to add a '$' to the end of each regex ...
e.g.: m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)$/
But still, thanks for that regex, that's exactly what I needed to create a validate ip script without using a cpan module (e.g Data::Validate::IP).
Thanks for the missing '$'. I will be sure and add it. Glad that it helped you with your project as well!
Hi Thanks for posting this script. It was so helpful. Iam using it in my project.
Hi,
To validate IPv4 addressing above code has a bug.
Regexp defined above is ( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)/ )
but for example IP provided is 1.12.123.1234, with above expression it is a valid IP address.
this is the correct regexp for validating Ipv4 addressing: ( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d{1,})/ )
Anyways thanks for the code. It is helping me alot.
( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d{1,})/ ) this will let something like 1.1.1.1e slip by. Instead add $ to the end ( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d{1,})$/ )
I did a script which satisfy the condition : 0 to 255. You can easily edit the script based on ur network class.
The below script will help u to some extent with help of regular expression.
!/usr/bin/perl
Pass the IP Address details
$ip = $ARGV[0];
if( $ip =~ /^((([01]?[0-9]{1,2}|[2][0-5]{1,2}).){3}([01]?[0-9]{1,2}|[2][0-5]{1,2}))$/ ) { print "IP:$ip: is valid\n"; } else { print "IP:$ip: is not valid\n"; }
You should prefer cpan packages if any .... In this case, there is one, so my recommendation is to use Data::Validate::IP to validate your string ... Here is link to docs: http://search.cpan.org/~neely/Data-Validate-IP-0.11/lib/Data/Validate/IP.pm
@veljkopopovic Your absolutely right, there is a module for that. But, at the time that I put this together I was learning and posted what was working for me. I preferred to re-invent the wheel, that's why I wrote this.
@j0sh097 Good point on the $. I never actually came back to this and corrected it. I did notice that fact and corrected it in my local scripts where I used it, but never updated it here. Thanks for pointing that out.
@dhineshkumar I would still chomp $ARGV as a "just in case", but that's me. :) Thanks!
@j0sh097 Just wanted to note that the version I used in the code actually catches the 1.1.1.1e and doesn't let it pass. The one you mentioned does let it slip, so I am glad I used the one I did. :)
use warnings;
print("What is the IP Address you would like to validate: "); my $ipaddr = ; chomp($ipaddr);
if( $ipaddr =~ m/^(\d\d?\d?).(\d\d?\d?).(\d\d?\d?).(\d\d?\d?)$/ ) { print("IP Address $ipaddr --> VALID FORMAT! \n");
Hello All,
discovered for dhineshkumar's example :
IP:10.10.10.1: is valid
IP:11111111: is valid
IP:172.16.0.248: is not valid
my modification is :
!/usr/bin/perl
Pass the IP Address details
$ip = $ARGV[0];
if(( $ip =~ /^((([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/ ) { print "IP:$ip: is valid\n"; } else { print "IP:$ip: is not valid\n"; }
sv posted a really great solution. It's clean, but I found a bugs with the code. 1) IP cannot start with 0 (zero). minor fix: /^((([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/
Then a nit pick (not really a bug), but if you think that an octet cannot start with 0 (zero) and then follow with additional digits. ie..192.168.1.023 or 192.168.1.00 fix: /^((([1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){3}([0]|[1-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]))$/
Hi i like the script going to use it in a project. thank you for posting it
Hi i like the script going to use it in a project. thank you for posting it
Hi i like the script going to use it in a project. thank you for posting it
Hi Friends, with the above regular expression, it will also match 0.0.0.0 so tell me a code that will not match any ip address which has "0" in it.