Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: IPAddress check (updated)

by haukex (Abbot)
on Mar 13, 2018 at 11:46 UTC ( #1210806=note: print w/replies, xml ) Need Help??


in reply to IPAddress check

"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 182.75.123.121/30"; 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 111.1.1.1 is a valid IP) or 256.255.255.256 (because 56.255.255.25 matches).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1210806]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2018-04-20 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?