in reply to pattern matching tricky one
That makes it a little clearer.$pattern = LP[SKTAQEHLDN][TA][GN][EDASTV] = 5/6
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:
Which prints:#!/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
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 :)
|
|---|