in reply to regex to match double letters but not triple or more

The regexp concept that maps to "... but not followed by ..." is a negative lookahead (?!...). In this case, you want to find a letter, require that it be followed by the same letter, but that that is not followed by the same letter again:

m{ ([A-Za-z]) # find a letter (and capture it) \1 # followed by the same (?!\1) # but not by the same again }x;

However, this will match "baaac", since it can match starting at the third character. To reject triples fully, we also need to specify that the first character we match doesn't have the same character before it.

Easiest would be if we could place a negative lookbehind first: /(?<!...) ([A-Za-z]) \1 (?!\1)/x. But that doesn't work in this case: we don't yet know what letter to reject.

Next easiest would be to capture the letter of interest, then use a negative lookbehind to check two characters back: the one we just captured and its predecessor. Unforunately that doesn't work either, since perl rejects m{([A-Za-z])(?<!\1.)} at compile time: earlier perls say "Variable length lookbehind not implemented", while more recent perls say "Lookbehind longer than 255 not implemented", in either case because they are not clever enough to determine at compile-time how long the capture can be.

So instead we have to work forward: if we're not at the start of the string, require that the notionally "preceding" character is different from our character of interest.:

m{ (?: # either ^ # start of string | # or (.) (?!\1) # any character that is not followed by a duplicat +e ) # now proceed as before, keeping in mind this is now the second ca +pture ([A-Za-z]) \2 (?!\2) }x;

Note that the second of these approaches only works if there is at least one character before the double letter, so will fail to match "aab" - probably not what you want in this case, so I show it only for completeness. The first of these approaches should work for all the cases you care about.

Update 2024-08-17: struck last paragraph, which was left over from initial editing.