Generally speaking, it's better to match any number, then make sure it's in range.

# Supports ranges in any number of bytes. sub parse_ip_mask { my ($ip_mask) = @_; my $checks = ''; my $check_num = 0; my $ip_re = join('\\.', map { if (/\[(\d+)-(\d+)\]/) { $check_num++; $checks .= " && \$$check_num >= $1"; $checks .= " && \$$check_num <= $2"; '(\\d+)' } else { $_ } } split(/\./, $ip_mask)); return eval "sub { (\$_[0] || \$_) =~ /^$ip_re\$/$checks }"; } my $ip_check = parse_ip_mask('131.202.1.[3-4]'); foreach (qw( 131.202.1.2 131.202.1.3 131.202.1.4 132.202.1.4 )) { print(&$ip_check() # Uses $_ if no arguments are specified. ? "$_ matches.$/" : "$_ doesn't match.$/" ); } __END__ output ====== 131.202.1.2 doesn't match. 131.202.1.3 matches. 131.202.1.4 matches. 132.202.1.4 doesn't match.

It's possible to do this without eval. Give me a few minutes.


In reply to Re: converting user friendly regex to perl friendly by ikegami
in thread converting user friendly regex to perl friendly by Random_Walk

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.