Duck Duck Go pointed me to this quite old thread when searching for "regular expression matching double letters". Although the OP emphasized "and only double letters" in its title all solutions posted so far also match double letters which are part of triple, quadruple, ... letters (e.g. in "Helllo", "cooool") and within 12 years no one seems to have cared about that. For what it's worth I do care about it and found it surprisingly difficult to come up with a regex that matches doubles only. One of my closest approaches is

/(.)((?!\1).)\2(?!\2)/

which uses a negative lookahead to make sure there's something else before the double letter and another negative lookahead to make sure there's something else following the double letter. However, this regex still matches the "something else before" character, too - as I have to use a capturing group for it in order to have a corresponding backreference to use in the lookahead assertion.

Could it be that finding doubles only is not a regular problem in the sense of Theoretical Computer Science? Or is it just me being unable to find a proper solution?


In reply to Re: Using regex to match double letters, and only double letters by Locutus
in thread Using regex to match double letters, and only double letters 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.