If you don't need a single regex to do all the work then something like the following might prove useful:
use strict; use warnings; my $string = 'A000 A123 A567 B789 B888 B999 B000 A998 A999'; my $regex = qr/((A|B)(\d{3}))/; while ( $string =~ /$regex/g ) { if ( $2 eq 'A' ) { if ( $3 == 000 or ($3 >= 123 and $3 <= 456) or $3 == 999 ) { print "$1 valid string\n"; } else { print "$1 rejected invalid range\n" } } elsif ( $2 eq 'B' ) { if ( $3 == 000 or ($3 >= 789 and $3 <= 888) ) { print "$1 valid string\n"; } else { print "$1 rejected invalid range\n" } } else { print "$3 seems valid to me\n" } }

update:
something else along the same lines:

while ( $string =~ /((?:A|B)\d{3})/g ) { if ( $1 eq 'A000' or ( $1 ge 'A123' and $1 le 'A456' ) or ( $1 ge 'A999' and $1 le 'B000' ) or ( $1 ge 'B789' and $1 le 'B888' ) ) { print "$1 matched\n"; } else { print "$1 did not match\n" }
or if you are going only for regex only solutions according to this the following regex should be valid in both:
/(A(?:000|(?:1(?:2[0-3]|[01]\d))|[23]\d{2}|4(?:[0-4]\d|5[0-6])|999)|B( +?:000|789|79\d|(?:8[0-7]\d|88[0-8]+)))/

-enlil


In reply to Re: Need Set-like Regex for Perl and Java (golf OK) by Enlil
in thread Need Set-like Regex for Perl and Java (golf OK) by QM

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.