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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.