$string > show box detail
2 boxes:
1) Box ID: 1
IP: 127.0.0.1*
Interface: 1/1
Priority: 31
* の追加チェックを使用して上記の文字列から IP を抽出する方法
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
IPのように見えるものは1つしかないため、次の正規表現を使用できます。
(?:([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])
これはDebuggexでどのように見えるかです。
それはファイルのように見えるので、私の最初の仮定は、あなたがワンライナーが欲しいということです:
perl -anlwe 'if ($F[0] eq "IP:") { print $F[1] }' input.txt
それ以外の場合は、次のようなソフト正規表現をお勧めします。
if ($foo =~ /IP:\s*(\S+)/) {
$ip = $1;
}
これを使用して一致させることができます (オプションの * で表示)
((\d{1,3}\.){3}\d{1,3}\*?)