I'm asking this on behalf of a friend, so I may be a little fuzzy on the details and requirements. [My friend is expert in Java, and somewhat familiar with Perl.]

I need a regex for a known subset of strings matching a single alpha, followed by 3 digits. For example, in set notation:

[A000,A123-A456,A999-B000,B789-B888]
As this is used in parallel Perl and Java apps, it would be greatly beneficial if the solution was similar in both languages. [I'm assuming that Java regexes are similar to Perl's.] Case doesn't matter (assume uppercase for convenience). Beware of crossing alpha boundaries (e.g., A999 to B000). The actual list of valid strings may be quite large. Golfed, but maintainable, solutions are also appreciated!

I first suggested the following:

my $regex = join '|', ( 'A000','A123'..'A456','A999'..'B000','B789'..' +B888' ); if ( $x =~ /^$regex$/o ) {...}
However, this backtracks a lot.

I suggested using a hash, but this was shot down by the Java equivalency requirement. [Is that a correct assessment?]

I think it's possible to use complicated (?{code}) or look-aheads, but I'm not sure if Java has an equivalent.

If I've missed something relevant in searching PerlMonks, please let me know.

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to 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.