Hello,

I am trying to write a script to take input in the form of "ABBCDE", and output all words which match that pattern. For example, "LOOKED" matches (note the repeated 'O', corresponding to the repeated 'B', and that all the other characters are mapped uniquely). Presently, my script takes the input an generates a regex. This is what it comes up with:

/^(\w)([^\1])(\2)([^\1\2\3])([^\1\2\3\4])([^\1\2\3\4\5])\s/

What I was trying to express with this is:

  1. . Any character...
  2. . ...followed by anything that isn't the first character...
  3. . ...followed by what was found in (2)
  4. . ...followed by anything that isn't the first, second, or third match...

And so forth. The problem is that this regex also matches words like "SEEMED". This is bad, since that third E doesn't correspond to the input pattern. I am unsure why my regex is failing. Can anyone shed some light?

- Anonymous Monk

In reply to 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.