Sometimes the most "fun" code to ponder is actually the worst code to maintain, or try to figure out after you haven't seen it for a long time.
I think this thread is a case in point. It is definitely *informative* to learn how to solve this problem using a RegEx, but that begs the question of whether you really *want* to use a RegEx.
I wouldn't, because, as you said yourself, it looks a bit ugly. It seems easier to just recognize that the word "ABBCBD" is a simple substitution cypher for "SEEMED" ... and then just try to write a very simple subroutine that helps us find different ways to "decrypt" our pattern "ABBCBD" (or whatever your pattern is) using an English dictionary.
It is less fun to do it the non RegEx way, but (arguably) easier to understand and debug (YMMV).
Here's one way to do it ... TMTOWTDI.
use strict; use warnings; my $sPattern; my @aPlainText = qw(looked seemed liked cooked booked baked balked); ### $sPattern = "ABBCBD"; ### try this one later $sPattern = "ABBCED"; while(<@aPlainText>){ print "$_\n" if IsEqual($sPattern,$_); } sub IsEqual { my $sPattern = shift || die "missing argument"; my $sPlain = shift || die "missing argument"; my $iLen = 0; my %hSubstit1 = (); my %hSubstit2 = (); if(length($sPattern) != length($sPlain)){return 0;} else{$iLen = length($sPattern)-1}; for (0 .. $iLen){ my $chrCiphr = lc(substr($sPattern,$_,1)); my $chrPlain = lc(substr($sPlain,$_,1)); ### require a 1 to 1 mapping between plain chars and pattern c +hars $hSubstit1{$chrPlain} = $chrCiphr unless exists $hSubstit1 +{$chrPlain}; $hSubstit2{$chrCiphr} = $chrPlain unless exists $hSubstit2 +{$chrCiphr}; if($chrCiphr ne $hSubstit1{$chrPlain }){return 0;} if($chrPlain ne $hSubstit2{$chrCiphr }){return 0;} } return(1); }###end_sub
In reply to Re: Matching Character Patterns With Backreferences
by dimar
in thread Matching Character Patterns With Backreferences
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |