Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

pattern matching tricky one

by Anonymous Monk
on May 12, 2006 at 21:54 UTC ( [id://549123]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!
Suppose you have the following string of letters :

$string ='LPSTEOPRTRYERTRETR';
and you want to search for the following pattern inside the string:
$pattern= LP[SKTAQEHLDN][TA][GN][EDASTV]       =>5/6
By saying 5/6, I mean that you can have one mismatch but still it must return success.
If you check the string above, it has L, P, S, T and then E. There is no G in the pattern, but I am ok with it...
So, my question is how can you set a threshold in pattern matching above which you will return success...

Edited by planetscape - added code tags

( keep:0 edit:16 reap:0 )

Replies are listed 'Best First'.
Re: pattern matching tricky one
by kvale (Monsignor) on May 12, 2006 at 22:01 UTC
    For general approximate pattern matching, I would recommend String::Approx.

    -Mark

Re: pattern matching tricky one
by McDarren (Abbot) on May 13, 2006 at 02:37 UTC
    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 :)

Re: pattern matching tricky one
by Anonymous Monk on May 12, 2006 at 22:08 UTC
    Hi Mark, thanx for your time!
    I have not used perl extensions until now... I will check on what you suggest... I thought it could be done with "traditional" pattern matching operators or something similar :(

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://549123]
Approved by kwaping
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found