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


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to