Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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();

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
Node Status?
node history
Node Type: note [id://642997]
help
Chatterbox?
[Lady_Aleena]: Corion, I knew that, I was just giving Discipulus a hard time.
choroba likes the story at the end of the node
[choroba]: reminds me of the days at the bank
[Corion]: Lady_Aleena: Hehe ;-D
[Lady_Aleena]: Corion, it was PerlMonks who kept pushing me to get Linux, so now I know some of the commands to use in word play.
[Corion]: Lady_Aleena: :-D
[Lady_Aleena]: Corion, so do you think the gods would be open to taking over Cookies should I disappear for too long a time?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2017-04-26 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (471 votes). Check out past polls.