http://qs321.pair.com?node_id=1212994


in reply to Using regex to match double letters, and only double letters

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?