Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by ikegami (Patriarch)
on Oct 05, 2007 at 22:49 UTC ( [id://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();

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://642997]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found