$string > show box detail
2 boxes:
1) Box ID: 1
IP: 127.0.0.1*
Interface: 1/1
Priority: 31
How to extract IP from above string withc additional check for * ?
$string > show box detail
2 boxes:
1) Box ID: 1
IP: 127.0.0.1*
Interface: 1/1
Priority: 31
How to extract IP from above string withc additional check for * ?
Since you only have one thing that looks like an ip, you can just use this regex:
(?:([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
This is what it looks like on Debuggex.
Since that looks like a file, my first assumption is that you want a one-liner:
perl -anlwe 'if ($F[0] eq "IP:") { print $F[1] }' input.txt
Otherwise, I would suggest a soft regex such as:
if ($foo =~ /IP:\s*(\S+)/) {
$ip = $1;
}
Use Regexp::Common:
#!/usr/bin/env perl
use 5.012;
use strict;
use warnings;
use Regexp::Common qw( net );
while (my $line = <DATA>) {
my @ips = ($line =~ /($RE{net}{IPv4})/g)
or next;
say @ips;
}
__DATA__
$string > show box detail
2 boxes:
1) Box ID: 1
IP: 127.0.0.1*
Interface: 1/1
Priority: 31
2) Box ID: 2
IP: 10.10.1.1
Interface: 1/1
Priority: 31
You can use this to match (shown with optional *)
((\d{1,3}\.){3}\d{1,3}\*?)