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:
- match a - followed by 2 4s, 5s, or js
- 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 |