RegEx: words with two letters repeated twice (eg. ABpoiuyAB, xnvXYlsdjsdXYmsd)

regex

Solution

Let's see:

`(\w{2})` matches two letters (or digits/underscore, but let's ignore that) and captures them in group number 1. Then `\1` matches whatever was matched by that group. So

\b\w*(\w{2})\w*\1

is what you're looking for (you don't need `{2,}` because if three letters are repeated, two letters are also repeated. Not checking for more than two makes the regex much more efficient. You can stop matching after the `\1` backreference has succeeded).

Problem

I had two regex tasks to do today -- and I did one properly and failed with the other. the first task was to find -- in a long, long text -- all the words beginning with "F" and ending with a vowel: ``` (\bf)\w*([euioay]\b) ``` and it worked perfectly. the second one is way too difficult for a philology student ;-) I have to find all the words with repeated at least twice two-letter sequences, for example: - tatarak is TATArak, "TA" twice; - brzozowski is brZOZOwski, "ZO" twice; - loremipsrecdks is loREmipsREcdks, "RE" twice; can I have some help please? thanks in advance ;-)

Original source