DevKits

Text

Match duplicate consecutive words

Flags: gi

Find repeated words like 'the the' — a common typo.

Pattern

Regular expression

\b(\w+)\s+\1\b

How it works

`\1` is a backreference to the first capture group. This finds any word followed by whitespace and then the same word again. Case-insensitive to catch `The the`.

Matches

  • the the
  • and And
  • very very

Non-matches

  • the cat
  • a b

Common gotchas

Go's built-in regexp package does not support backreferences (RE2 syntax). Use the `regexp2` third-party package for backreferences, or accept the limitation.

Language snippets

JavaScript

const re = /\b(\w+)\s+\1\b/gi;
text.match(re);

Python

re.findall(r"\b(\w+)\s+\1\b", text, re.I)

Go

// import "github.com/dlclark/regexp2" — stdlib regexp lacks backreferences

Java

Pattern.compile("\\b(\\w+)\\s+\\1\\b", Pattern.CASE_INSENSITIVE)

Related patterns

Try any pattern live in the Regex Tester