in reply to Match zero times in regex
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Match zero times in regex
by ikegami (Patriarch) on Dec 13, 2011 at 04:26 UTC | |
by AnomalousMonk (Archbishop) on Dec 13, 2011 at 05:02 UTC | |
by ikegami (Patriarch) on Dec 17, 2011 at 09:20 UTC | |
by AnomalousMonk (Archbishop) on Dec 17, 2011 at 21:13 UTC | |
|
Re^2: Match zero times in regex
by ricDeez (Scribe) on Dec 13, 2011 at 03:39 UTC | |
by AnomalousMonk (Archbishop) on Dec 13, 2011 at 04:47 UTC | |
by ricDeez (Scribe) on Dec 13, 2011 at 07:38 UTC | |
|
Re^2: Match zero times in regex
by SomeNetworkGuy (Sexton) on Dec 13, 2011 at 03:36 UTC |