Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: searching for a string w/ a * in any single position?

by ikegami (Pope)
on Oct 05, 2007 at 22:49 UTC ( #642997=note: print w/ replies, xml ) Need Help??


in reply to Re: searching for a string w/ a * in any single position?
in thread searching for a string w/ a * in any single position?

I think the following will do the trick:

my @chars = map quotemeta, split //, $word; my $re = join '|', map { local @_ = @chars; $_[$_] = '.'; join '', @_ } 0..$#chars;

Using Regexp::Assemble should be faster:

use Regexp::Assemble qw( ); my @chars = map quotemeta, split //, $word; my $ra = Regexp::Assemble->new(); for (0..$#chars) { local @_ = @chars; $_[$_] = '.'; $ra->add(join '', @_) } my $re = $ra->re();


Comment on Re^2: searching for a string w/ a * in any single position?
Select or Download Code
Re^3: searching for a string w/ a * in any single position?
by blokhead (Monsignor) on Oct 05, 2007 at 23:06 UTC
    Generalizing this approach for Hamming distance > 1, using the combinations iterator from Iterating over combinations:
    sub gen_regex { my ($target, $n) = @_; my @chars = split //, $target; my $ra = Regexp::Assemble->new(); my $iter = combinations( $n => [ 0 .. $#chars ] ); while (my @c = $iter->()) { local @_ = @chars; $_[$_] = '.' for @c; $ra->add( join '', @_ ); } $ra->re(); }

    blokhead

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-10-23 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls