IP address matching is a bit tricky, so it's often helpful to turn to accumulated wisdom. Regexp::Common and Regexp::Common::net can help. (To see the trickiness, change one of the 255s in the example data of the OP to 256 – or even 666 or 66666 – and see the result using the OP regex.)
>perl -wMstrict -le "use Regexp::Common qw(net); my $IPv4 = qr{ (?<! \d) $RE{net}{IPv4} (?! \d) }xms; ;; my @strs = ( 'permit ip host 10.11.1.1 192.168.100.0 0.0.0.255', 'permit ip 10.11.1.0 0.0.0.255 192.168.100.0 0.0.0.255', ); ;; for my $s (@strs) { print qq{from: '$s'}; while ($s =~ m{ (?<! host) \s+ ($IPv4) \s+ ($IPv4) }xmsg) { print qq{IP pair: '$1' '$2'}; } } " from: 'permit ip host 10.11.1.1 192.168.100.0 0.0.0.255' IP pair: '192.168.100.0' '0.0.0.255' from: 'permit ip 10.11.1.0 0.0.0.255 192.168.100.0 0.0.0.255' IP pair: '10.11.1.0' '0.0.0.255' IP pair: '192.168.100.0' '0.0.0.255'
Update: Unfortunately, this solution has a bug. See Re^2: Match zero times in regex for counter-example data demonstrating it.
In reply to Re: Match zero times in regex
by AnomalousMonk
in thread Match zero times in regex
by SomeNetworkGuy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |