I'm not sure I understand what you want your regex to match: JUST THOSE THINGS IN THE LIST, or things of the same form as the ones in your list? It makes a difference, because if you think of the *patterns* you want to match first, then it's easier to craft the regex.

Anyhoo, just for fun, let's match just those things you've listed. I have a trick I use for short lists of different patterns, using join and an array of patterns I want to match:

my @patterns = qw(-[45j]{2} [1-3][a46]u); my $pat_string = join "|", @patterns; if ($MType =~ /^$pat_string/oi) { # do stuff }

yeah, those regexen are ugly, aren't they? But here's what they say:

  1. match a - followed by 2 4s, 5s, or js
  2. match the digits 1-3, followed by an a, 4 or 6, followed by a u

The join gives you alternation (yields up a match if the string contains either pattern); the /oi says to compile the regex once (not really necessary, but hey, it's a feature, so we use it)

I do not recommend this for code that needs to be optimized (alternations are generally slower than the system you're trying, where you try matching first one thing, then the other).

But I'm too lazy just at the moment to produce the more optimized version of this code (hint: it involves looping over the patterns, maybe using eval if you're really militant about optimizing)

HTH

Philosophy can be made out of anything. Or less -- Jerry A. Fodor


In reply to Re: Regexp experts, come to rescue! by arturo
in thread Regexp experts, come to rescue! by bman

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.