First of all, the lack of <code> tags makes your question very unclear, which is probably why you haven't had many responses.
$pattern = LP[SKTAQEHLDN][TA][GN][EDASTV] = 5/6
That makes it a little clearer.
I've been having a bit of a play around with this, and one approach that you could take is to turn each of your alternations into captures, and make them optional. So you'd do something like (L)?, then (P)?, then ([SKTAQEHLDN])?, etc.
If you assign the match(es) directly to an array variable, you'll end up with a six element list where each defined element represents a successful match. Then it's simply a matter of counting the number of defined elements. If you have more than 4, it's a successful overall match.
Here is some sample code to demonstrate what I mean:
#!/usr/bin/perl -wl
use strict;
while (my $string = <DATA>) {
chomp($string);
my @matches = $string =~ /(L)?(P)?([SKTAQEHLDN])?([TA])?([GN])?([E
+DASTV])?/;
my $num_matches;
for (@matches) {
$num_matches++ if defined($_);
}
if ($num_matches > 4) {
print "$string matches ($num_matches/6)";
}
else {
print "$string does not match ($num_matches/6)";
}
}
__DATA__
LPSTEOPRTRYERTRETR
HELLO WORLD
PERL MONKS
Which prints:
LPSTEOPRTRYERTRETR matches (5/6)
HELLO WORLD does not match (2/6)
PERL MONKS does not match (2/6)
Note that the above could probably be "golfed" significantly, but I've deliberately kept it verbose to make it obvious what's happening.
Cheers,
Darren :) |