Here I show you how to use a backreference in regex to be able to find double words in regex. What you’ll learn how to do is to recognize where a word is doubled in a sentence, like: I have have a sentence here. You can see that the word ‘have’ is doubled. This is a simple situation that we can use to build more involved scenarios on so it’s worth taking the time here to break it down so that you understand the basics of this construct before moving on to the complex scenarios.
That said, we’re going to use an inline backreference. Typically whenever we use a backreference, we use it in the replacement text to access the capture group. However, here we’re going to use it in the search pattern itself so we can reference it later in the search. That’s why this is a backreference; because it finds a match and captures it, then it looks for that match again later in the regex expression.