This is a bit clunky but I think it should do what you want. I am going to see if I can optimise out some cruft.

#!/usr/local/bin/perl -w use strict; my %ipranges; while (<DATA>) { next if /^\s*$/; last if /END CONFIG/; chomp; my @ranges = map { if (/^\[(\d+)-(\d+)\]$/) {$1, $2} else {$_, $_}} split /\./; $ipranges{$_}=\@ranges; } while (<DATA>) { next if /^\s*$/; chomp; my $ip=$_; RANGES: foreach my $range (keys %ipranges) { my @eatme=@{$ipranges{$range}}; foreach (split /\./, $ip) { my $min = shift @eatme; my $max = shift @eatme; next RANGES unless ($_ >= $min and $_ <= $max) } print "A match of $ip in range $range\n"; } } __DATA__ 123.145.141.2 123.145.[146-149].2 135.168.[10-115].[0-125] END CONFIG 1.2.3.4 123.145.147.2 123.145.147.4 135.168.102.102 __END__ results A match of 123.145.147.2 in range 123.145.[146-149].2 A match of 135.168.102.102 in range 135.168.[10-115].[0-125]

updated code

After a little decrufting and improving (or is that obfuscating) the data structure I have this..
/usr/local/bin/perl -w use strict; my %ranges; while (<DATA>) { next if /^\s*$/; last if /END CONFIG/; chomp; $ranges{$_}=[ map { if (/^\[(\d+)-(\d+)\]$/) {[$1, $2]} else {[$_, $_]}} split /\./]; } while (<DATA>) { next if /^\s*$/; last if /END/; chomp (my $ip=$_); RANGE: foreach my $range (keys %ranges) { my $i=0; foreach (split /\./, $ip) { next RANGE unless ($_ >= $ranges{$range}->[$i]->[0] and $_ <= $ranges{$range}->[$i++]->[1]); } print "$ip\tmatches range $range\n"; } }

Cheers,
R.


In reply to Re: IP Filtering RegEx needed by Random_Walk
in thread IP Filtering RegEx needed by Anonymous Monk

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.