I need a regex for a known subset of strings matching a single alpha, followed by 3 digits. For example, in set notation:
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![A000,A123-A456,A999-B000,B789-B888]
I first suggested the following:
However, this backtracks a lot.my $regex = join '|', ( 'A000','A123'..'A456','A999'..'B000','B789'..' +B888' ); if ( $x =~ /^$regex$/o ) {...}
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |