match all but once? 
Author Message
 match all but once?

Hello,
I am trying to figure out how to make a regexp that will take two
strings and will see if all but one of the characters match, the
matching ones must be in the same location in the word (the two
strings are same length) but it doesn't matter where the differing
character is.
For example:
"dog"
This would need to match:
gog, bog, dod, dig ...etc.
But not match:
god, day, too

Each time only one character is different and the others are the same.

Your help is greatly appreciated,
Harley



Sat, 22 Oct 2005 13:07:47 GMT  
 match all but once?
Quote:

> Hello,
> I am trying to figure out how to make a regexp that will take two
> strings and will see if all but one of the characters match, the
> matching ones must be in the same location in the word (the two
> strings are same length) but it doesn't matter where the differing
> character is.

[snip]

1) Don't post in comp.lang.perl, it doesn't exist.

2) There are a number of approximate match methods and modules, check
Google, CPAN, etc.

3) Why do you need a regex? Why not something else?

4) Something quick and dirty with a regex might be:

sub char_diff_count
{
  my $p = shift;
  my $q = shift;

  # bitwise xor, then delete nulls
  ( my $r = $p ^ $q ) =~ s/\0//g;

  # count remaining chars
  return length( $r );

Quote:
}

my $x = 'can';
my $y = 'con';
my $z = 'cane';

print "$x $y ", char_diff_count( $x, $y ), "\n"; # can con 1
print "$x $z ", char_diff_count( $x, $z ), "\n"; # can cane 1
print "$y $z ", char_diff_count( $y, $z ), "\n"; # con cane 2
print "$x $x ", char_diff_count( $x, $x ), "\n"; # can can 0

__END__

I'll leave it to OMAR to work out the logical test for "differ by 1".

-QM



Sun, 23 Oct 2005 09:37:32 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Matching two patterns at once

2. Finding all regexps matches at once?

3. FAQ 6.17 How do I efficiently match many regular expressions at once?

4. FAQ 6.17 How do I efficiently match many regular expressions at once?

5. FAQ 6.17 How do I efficiently match many regular expressions at once?

6. FAQ 6.17 How do I efficiently match many regular expressions at once?

7. FAQ 6.17 How do I efficiently match many regular expressions at once?

8. FAQ 6.17 How do I efficiently match many regular expressions at once?

9. FAQ 6.17 How do I efficiently match many regular expressions at once?

10. FAQ 6.16: How do I efficiently match many regular expressions at once?

11. FAQ 6.16: How do I efficiently match many regular expressions at once?

12. FAQ 6.16: How do I efficiently match many regular expressions at once?

 

 
Powered by phpBB® Forum Software