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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.