Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: string match using with an N in any position

by bart (Canon)
on Nov 18, 2011 at 11:55 UTC ( #938792=note: print w/ replies, xml ) Need Help??


in reply to Re: string match using with an N in any position
in thread string match using with an N in any position

Here's a snippet of code that implements what you describe:

sub fuzzy { my $string = shift; my @alt; for(my $i = 0; $i < length($string); $i++) { my $alt = $string; substr($alt, $i, 1) = 'N'; push @alt, $alt; } return join '|', $string, @alt; # original too } print fuzzy('GCGAT');
You can just do
$re = fuzzy($string); if($value =~ /^($re)/) { ... }
Note the parens for grouping.

If there's only one value for $re during the run of the file, you can use /o (as in /^($re)/o) but in a modern perl I don't think that makes much difference.


Comment on Re^2: string match using with an N in any position
Select or Download Code
Re^3: string match using with an N in any position
by Marshall (Prior) on Nov 22, 2011 at 00:09 UTC
    Yes, this is the idea!
    I was out of town and using a friend's laptop without Perl installed and I was reluctant to post "untested code". This idea applies also in much more complex situations. I have one function that can generate regex'es with 4-12 terms from very similar length "search for" strings due to the "rules".

    Anyway if the "rules" can be described as a regex generation algorithm, then I am suggesting in this thread an "additional tool to add to the Perl toolbox".

    This had the added benefit of when the end user thinks that something should have either matched or not matched, the test output of the module can print regex'es for test cases (mine does). And the terms are simple enough that a user familiar with Windows '.', and '*' wildcards can tell me some new case to add or delete.

    This "write the regex" code replaced what my working group came to call "the regex from hell" - so complicated that you might have to read Mastering Regular Expressions by Jeffrey-Friedl several times before you could understand it!

      Here's another idea: Regexp::Assemble is a module that can combine regexps — and possibly optimize the result. Granted, nowadays perl already optimizes large regexps, so it's not as useful any more as it once was.

      Turning a glob pattern into a regexp isn't that hard: you can already get far by replacing every "?" with ".", "*" with ".*", and quotemeta everything else.

      %s = ( '?' => '.', '*' => '.*' ); s/(\W)/$s{$1} || "\\$1"/ge;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938792]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-12-29 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls