FAQ
Hiya

I was hoping that someone would be kind to help me.

I have a string like so :

Haresources : 10.203.4.5, Interfaces : 10.203.4.5 10.203.4.7

Im trying to get the ip's after Interfaces into an array, but for the
likes of me, im just not getting it right. This is what I currently got.

my @h2ip = ($tmp{ $opt_H2 } =~
/(?:Interfaces\s:\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/gi );

If someone could help, I would be most appreciative.

Kind Regards
Brent Clark

Search Discussions

  • Dermot at Nov 2, 2009 at 4:31 pm

    2009/11/2 Brent Clark <brentgclarklist@gmail.com>:
    Hiya

    I was hoping that someone would be kind to help me.

    I have a string like so :

    Haresources : 10.203.4.5, Interfaces : 10.203.4.5 10.203.4.7

    Im trying to get the ip's after Interfaces into an array, but for the likes
    of me, im just not getting it right. This is what I currently got.

    my @h2ip = ($tmp{ $opt_H2 } =~
    /(?:Interfaces\s:\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/gi );

    If someone could help, I would be most appreciative.
    This is probably not the most elegant solution but you could capture
    everything after 'Interface\s+:\s+(.*)' and the split it on the
    white-space.

    (my $h2ip) = ($str =~ m/Interfaces\s+:\s+(.*)/);
    my @h2ip = split(/\s+/, $h2ip);

    HTH,
    Dp.
  • Matt at Nov 2, 2009 at 5:15 pm

    On Nov 2, 9:54 am, brentgclarkl...@gmail.com (Brent Clark) wrote:
    Hiya

    I was hoping that someone would be kind to help me.

    I have a string like so :

    Haresources : 10.203.4.5, Interfaces : 10.203.4.5 10.203.4.7

    Im trying to get the ip's after Interfaces into an array, but for the
    likes of me, im just not getting it right. This is what I currently got.

    my @h2ip = ($tmp{ $opt_H2 } =~
    /(?:Interfaces\s:\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/gi );

    If someone could help, I would be most appreciative.

    Kind Regards
    Brent Clark
    If you will always have one and only one ip address before
    "Interfaces," this should work:

    my $string = 'Haresources : 10.203.4.5, Interfaces : 10.203.4.5
    10.203.4.7';
    my @h2ip = ($string =~ m/((\d{1,3}\.){3}\d{1,3})/gi);

    for (my $i = 2; $i < @h2ip; $i+=2) { print "$h2ip[$i]\n"; }

    $ ./test.pl
    10.203.4.5
    10.203.4.7

    Or, if you don't mind the longer regex and don't want to have to skip
    array indices:

    my $string = 'Haresources : 10.203.4.5, Interfaces : 10.203.4.5
    10.203.4.7';
    my @h2ip = ($string =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/gi);

    for (my $i = 1; $i < @h2ip; ++$i) { print "$h2ip[$i]\n"; }

    Matt
  • Tom smith at Nov 2, 2009 at 8:21 pm

    On Mon, Nov 2, 2009 at 7:54 AM, Brent Clark wrote:

    Hiya

    I was hoping that someone would be kind to help me.

    I have a string like so :

    Haresources : 10.203.4.5, Interfaces : 10.203.4.5 10.203.4.7

    Im trying to get the ip's after Interfaces into an array, but for the likes
    of me, im just not getting it right. This is what I currently got.

    my @h2ip = ($tmp{ $opt_H2 } =~
    /(?:Interfaces\s:\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/gi );

    If someone could help, I would be most appreciative.

    Kind Regards
    Brent Clark


    Here are a couple more options:

    use strict;
    use warnings;

    $\ = "\n";
    $, = ", ";

    my $str = 'Haresources : 10.203.4.5, Interfaces : 10.203.4.5 10.203.4.7';

    my @pieces = split " : ", $str;
    my @ips = split " ", $pieces[-1];

    foreach my $ip(@ips) {
    print "--->$ip<----";
    }

    --output:--
    --->10.203.4.5<----
    --->10.203.4.7<----


    @pieces = split "Interfaces", $str;
    my $target = $pieces[-1];
    print $target;

    while ($target =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g) {
    print "--->$1<---";
    }

    --output:--
    : 10.203.4.5 10.203.4.7
    --->10.203.4.5<---
    --->10.203.4.7<---
  • John W. Krahn at Nov 3, 2009 at 8:32 pm

    Brent Clark wrote:
    Hiya Hello,
    I was hoping that someone would be kind to help me.

    I have a string like so :

    Haresources : 10.203.4.5, Interfaces : 10.203.4.5 10.203.4.7

    Im trying to get the ip's after Interfaces into an array, but for the
    likes of me, im just not getting it right. This is what I currently got.

    my @h2ip = ($tmp{ $opt_H2 } =~
    /(?:Interfaces\s:\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/gi );

    If someone could help, I would be most appreciative.
    my @h2ip = $tmp{ $opt_H2 } =~ /Interfaces\s*:/ig && $tmp{ $opt_H2 } =~
    /(?<=\D)\d{1,3}(?:\.\d{1,3}){3}(?=\D|$)/g;



    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
  • John W. Krahn at Nov 3, 2009 at 9:23 pm

    John W. Krahn wrote:
    Brent Clark wrote:
    I was hoping that someone would be kind to help me.

    I have a string like so :

    Haresources : 10.203.4.5, Interfaces : 10.203.4.5 10.203.4.7

    Im trying to get the ip's after Interfaces into an array, but for the
    likes of me, im just not getting it right. This is what I currently got.

    my @h2ip = ($tmp{ $opt_H2 } =~
    /(?:Interfaces\s:\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))/gi );

    If someone could help, I would be most appreciative.
    my @h2ip = $tmp{ $opt_H2 } =~ /Interfaces\s*:/ig && $tmp{ $opt_H2 } =~
    /(?<=\D)\d{1,3}(?:\.\d{1,3}){3}(?=\D|$)/g;
    That would definitely be better as:

    my @h2ip = $tmp{ $opt_H2 } =~ /Interfaces\s*:/ig ? $tmp{ $opt_H2 } =~
    /(?<=\D)\d{1,3}(?:\.\d{1,3}){3}(?=\D|$)/g : ();



    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 2, '09 at 2:54p
activeNov 3, '09 at 9:23p
posts6
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase