PatternRegex

Find doubled words

The proofreading classic: a backreference that catches accidentally doubled words like "the the" — even across a line break.

Last updated

Patterngi
\b(\w+)\s+\1\b
Open in Regex Tester & Builder

Matches

  • It was was a mistake
  • The the report

Doesn’t match

  • the theory
  • over and over

Why it’s written this way

The parentheses capture a word; \1 demands the exact same text again after whitespace. That is the whole trick — a backreference matches what the group MATCHED, not what it could match.

The i flag makes the backreference case-insensitive too, so sentence-start doubles like "The the" are caught. \s+ spans newlines, which is exactly where doubled words hide after editing.

Edge cases to know

  • →The trailing \b is load-bearing: without it, "the theory" would count as a double.
  • →Legitimate doubles exist — "had had", "that that" — so this is a reviewing aid, not a safe auto-replace.
  • →Words split by punctuation ("the, the") don't match; \s+ only crosses whitespace.

Part by part

\b(\w+)
A word, captured into group 1. The boundary at the front stops the match starting mid-word, which would find false repeats inside longer words.
\s+
One or more whitespace characters between the two words — so a doubled word split across a line break is still caught.
\1
A backreference to whatever group 1 captured. This is the whole mechanism: it does not match a word, it matches the same word again.

Common mistakes

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

    A single whitespace character misses the most common real case, where the duplicate sits across a line break or after a double space — exactly the places an editing mistake survives a read-through.

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

    Without boundaries the pattern matches inside longer words, so "the theme" reports "the" as doubled because the capture matches the start of the second word.

  • \b(\w+)\s+\w+\b

    Forgetting the backreference and writing a second \w+ instead matches any two consecutive words, so every sentence in the document is a hit. The whole mechanism here is \1 meaning "that same word again".

Questions

Does it catch legitimate repetition?

It flags it, yes — "had had", "that that" and "is is" are all grammatical in the right sentence, and this pattern cannot tell them from a typo. Treat every hit as a question rather than a correction.

Why does it need the i flag?

Because a backreference is case-sensitive by default, so "The the" would not match without it. Since the sentence-boundary case is the most common real duplication, the flag is doing most of the work here.

Will it find three of the same word in a row?

It finds the first pair and then resumes after it, so "the the the" reports one match rather than two. Running the search again on the corrected text catches the remainder, which is usually simpler than complicating the pattern.

Does it work across a punctuation mark?

No. \s+ matches whitespace only, so "the, the" is not flagged. That is deliberate — a repeated word separated by punctuation is far more often correct than not.

Related in Patterns