akr8986 has asked for the wisdom of the Perl Monks concerning the following question:
hi i am new to perl and was trying to write a script to check if ip address is valid. Though i found some solutions online i wanted to try it on my own
#! /usr/bin/perl -wprint "Enter IP Address:"; my $ip = <STDIN>; if ( $ip =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) { print "yes match $1 $2 $3 $4\n"; } else { print "no match\n"; }
when i enter 10.3.4.5 it says ip address matches as expected. But when enter 1000.3.4.5 too it says match is found and the values of $1 to $4 are printed as 000 3 4 5. How could this happen as i am saying to match only 3 digits and not more than that using "\d{1-3}" englighten me!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Validate Ip address Regexp
by ww (Archbishop) on Nov 28, 2015 at 12:10 UTC | |
by akr8986 (Initiate) on Nov 28, 2015 at 12:24 UTC | |
by AnomalousMonk (Archbishop) on Nov 28, 2015 at 13:52 UTC | |
|
Re: Validate Ip address Regexp
by neilwatson (Priest) on Nov 28, 2015 at 13:36 UTC | |
by AnomalousMonk (Archbishop) on Nov 28, 2015 at 14:00 UTC | |
|
Re: Validate Ip address Regexp
by BillKSmith (Monsignor) on Nov 28, 2015 at 13:28 UTC | |
|
Re: Validate Ip address Regexp
by Laurent_R (Canon) on Nov 28, 2015 at 16:31 UTC | |
|
Re: Validate Ip address Regexp
by VinsWorldcom (Prior) on Nov 28, 2015 at 14:11 UTC | |
|
Re: Validate Ip address Regexp
by shmem (Chancellor) on Nov 28, 2015 at 19:19 UTC | |
by Laurent_R (Canon) on Nov 28, 2015 at 22:53 UTC |