I'm not much in favor of regexes to solve problems like this in one swell foop. So long as the number of elements is sane, I'd build a hash and check for existence of a key. A regex is used to recognise ranged data and parse the boundary elements:

#!/usr/bin/perl my $set = [qw/A000 A123-A456 A999-B000 B789-B888/]; my %elements; for (@$set) { /-/ or $elements{$_} = undef or next; if ( /([[:alpha:]]\d{3})-([[:alpha:]]\d{3})/ ) { my $start = $1; do { $elements{$start} = undef } while $start++ ne $2; } } while (<DATA>) { chomp; print "$_ is ", (exists $elements{$_} ? '' : 'not '), 'in the set.', $/; } __DATA__ A000 A001 A122 A123 A124 A154 A320 A455 A456 A457 A530 A779 A932 A998 A999 B000 B001 B123 B666 B788 B789 B790 B820 B887 B888 B889 B900
That's a typical trade of memory for speed. The loop populating the %elements hash makes use of magical string increment to roll A over to B.

I should call attention to the difference between

do { $elements{$start} = undef } while $start++ ne $2;
and
$elements{$start} = undef while $start++ ne $2;
The latter will test and increment $start before any processing is done, while the do {} while cond; construction does the first pass unconditionally.

There is also a tricky bit of logic in the single element handling. . . . or next; is used to go to the next iteration unconditionally because the preceeding expression is always false.

After Compline,
Zaxo


In reply to Re: Need Set-like Regex for Perl and Java (golf OK) by Zaxo
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.