gcazianfnf has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a script that gives system values in a csv file. One such value I am trying to get is the system IP address. In AIX, I run the lsattr command:
root>$ lsattr -El en0|grep "Internet Address" netaddr 10.1.4.43 Internet Address True
I want to extract the IP address with the following code:
my $ipadd = `lsattr -El en0|grep "Internet Address"`; chomp($ipadd); if ($ipadd =~ m/^([0-255].[0-255].[0-255].[0-255])+/) { $ipaddress = $1; }
However, the value for $1 is null. Is my regexp pattern wrong?

Replies are listed 'Best First'.
Re: regexp for extracting IP address
by hardburn (Abbot) on Oct 11, 2004 at 20:32 UTC

    Character class ranges only go up to the first number. So your character classes are asking for (AFAICT) 0-2 or 5 (the second 5 is ignored). Also, the regex is set to start matching at the beginning of the string, but the IP occurs in the middle. Lastly, the dot isn't doing what you think it's doing; it matches any character, not just a dot.

    As long as your system guarentees that the lsattr command returns the address in dotted-quad form, this should work:

    my $dotted_quad = do { my $allowed_numbers = join '|', 0 .. 255; my $quad = qr/(?: $allowed_numbers )/x qr/ $quad \. $quad \. $quad \. $quad /x; }; . . . if( $ipadd =~ /($dotted_quad)/ ) { $ipaddress = $1; }

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: regexp for extracting IP address
by BUU (Prior) on Oct 11, 2004 at 20:33 UTC
    Yes, your regexp pattern is wrong. [0-255] does not match litterally the characters 0,1,2 through 255, but instead 0 thourgh 2 and 5, which is 3 characters.

    There are regexes out there that will only match a valid ip, but they are at least 5 lines long and really fairly ugly. Since you know the lsattr command is never going to return an invalid ip address, your best bet is to just construct a regex that matches things resembeling an IP, for exampe: /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
Re: regexp for extracting IP address
by Roy Johnson (Monsignor) on Oct 11, 2004 at 20:34 UTC
    The [] construct is a character class. Thus [0-255] matches 0, 1, 2, or 5. This very thing came up in a recent thread.

    Caution: Contents may have been coded under pressure.
Re: regexp for extracting IP address
by skx (Parson) on Oct 11, 2004 at 20:37 UTC

    Your problem appears to be the range that you are using - instead of using a character range you're trying to match against an invalid '2-5' + 5'.

    Instead use '[0-9]+' to match against one or more numerical digits.

    The following code matches against your input:

    my $input = "netaddr 10.1.4.43 Internet Address True"; my $ipaddress = "unknown"; if ( $input =~ /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ ) { $ipaddress = $1; } print $ipaddress . "\n";
    Steve
    ---
    steve.org.uk
      Simplify and lock down a little further:
      /(?:\d{1,3}\.){3}\d{1,3}/;
      This covers 256-999 as well, but is pretty much what you need in most instances.

      cLive ;-)

Re: regexp for extracting IP address
by reneeb (Chaplain) on Oct 12, 2004 at 08:35 UTC
    I think you are searching for Regexp::Common::net

    It's a great module that provides regexes for IPv4-Addresses.