Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

IPAddress check

by vasanth.easyrider (Acolyte)
on Mar 13, 2018 at 11:32 UTC ( #1210803=perlquestion: print w/replies, xml ) Need Help??
vasanth.easyrider has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks

i have a perl script

#!/usr/bin/perl use strict; use warnings; my $output = "ae4.5784 up down inet"; if($output =~ m/(\d{1,3}\.\d{1-3}\.\d{1-3}\.\d{1-3})$/g) { print "There is an IPdressAd in the output string\n"; } else { print "There is no IPAddress in the output string"; }

My requirement is to check if $output has an IPaddress in it or not. In above example - it contains an IP.
how to give the correct regular expression . The regular expression i have given is not working

Replies are listed 'Best First'.
Re: IPAddress check (updated)
by haukex (Abbot) on Mar 13, 2018 at 11:46 UTC

    "is not working" is not really an adequate problem description. Please see I know what I mean. Why don't you?

    But take a look at your regular expression: \d{1,3} vs. \d{1-3} - why the difference? One is right, the other is wrong. See perlrequick and perlretut.

    Note that even if you fix that, the regular expression will still match invalid IP's like 999.999.999.999. Instead of reinventing the wheel, I'd recommend Regexp::Common::net:

    use warnings; use strict; use Regexp::Common qw/net/; my $output = "ae4.5784 up down inet"; if ( $output =~ /\b$RE{net}{IPv4}\b/ ) { print "There IS an IPAddress in the output string\n"; } else { print "There is NO IPAddress in the output string\n"; }

    Update: Added the word boundary \b to the above regex, because otherwise it would have matched something like 1111.1.1.1 (because is a valid IP) or (because matches).

Re: IPAddress check
by thanos1983 (Vicar) on Mar 13, 2018 at 11:48 UTC

    Hello vasanth.easyrider,

    This question has been asked again Validate Ip address Regexp. I would use a module such as Regexp::Common.

    Sample of code from the Validate Ip address Regexp given by neilwatson

    #!/usr/bin/perl use strict; use warnings; use Regexp::Common qw/ net /; my $output = "ae4.5784 up down inet"; if ( $output =~ m/$RE{net}{IPv4}/ ){ print "match!\n" } __END__ $ perl match!

    Update: See update of fellow Monk haukex above for the correct usage.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Please note haukex's Update here about using a boundary assertion of some kind. The raw regex  $RE{net}{IPv4} used in the code example here matches the string
          my $output = "ae4.5784     up    down inet     99999.75.123.99999/30";
      which is problematic for containing a valid IP.

      Give a man a fish:  <%-{-{-{-<

        Hello AnomalousMonk,

        Thank you for this minor but important notice. :)

        BR / Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1210803]
Front-paged by Corion
and the monks are chillaxin'...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2018-06-25 20:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.