\b(\w+)\s+\1\b This pattern finds accidentally repeated words like 'the the' by capturing a word and backreferencing it after whitespace. The case-insensitive flag catches 'The the' as well, making it a handy proofreading check. Text patterns operate on prose and whitespace: finding repeated words, trimming trailing spaces, collapsing blank lines or restricting characters to a safe set. They are the workhorses of editors, linters and cleanup pipelines. Because natural text is messy, these patterns favor pragmatism and are usually applied with global/multiline flags across many lines at once. It uses the gi flag(s). Apply it across text to collect every occurrence. \w includes digits and underscores, so '1 1' matches; restrict to [A-Za-z] if you only want alphabetic words. It catches intentional repetition (e.g. 'ha ha') as a false positive — review matches rather than auto-deleting. Backreference \1 matches the exact captured text; with the i flag it is case-insensitive, which is usually desired for prose. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.
Token-by-token explanation
\b — word boundary before the first word(\w+) — capture a word (letters, digits, underscore)\s+ — one or more whitespace characters between the words\1 — backreference to the same captured word\b — word boundary after the repeatgi flags — global and case-insensitive
What it matches
the theis ishello helloThe the What it does not match
the themhello worldthe theaterunique words When to use it
Reach for a text pattern when cleaning or analyzing prose: trimming trailing whitespace, collapsing blank lines, finding duplicated words or restricting characters to a safe set. Apply it with global and multiline flags across the whole document in one pass. Because natural language is irregular, treat matches as candidates for review rather than automatic edits, and test on representative samples so you do not mangle legitimate content such as intentional repetition or code blocks.
Caveats
- \w includes digits and underscores, so '1 1' matches; restrict to [A-Za-z] if you only want alphabetic words.
- It catches intentional repetition (e.g. 'ha ha') as a false positive — review matches rather than auto-deleting.
- Backreference \1 matches the exact captured text; with the i flag it is case-insensitive, which is usually desired for prose.
Code examples
const re = /\b(\w+)\s+\1\b/gi;
const text = "see the the and more";
const found = [...text.matchAll(re)].map(m => m[1] ?? m[0]);
// found holds every extracted occurrence import re
rx = re.compile(r"\b(\w+)\s+\1\b", re.I)
text = "see the the and more"
found = rx.findall(text) # every occurrence (groups if present) import java.util.regex.*;
Pattern p = Pattern.compile("\\b(\\w+)\\s+\\1\\b", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("the the");
boolean ok = m.find(); // scan for occurrences <?php
$subject = "the the";
if (preg_match_all('/\b(\w+)\s+\1\b/i', $subject, $m)) {
// $m[0] is the full match; $m holds every occurrence
} How to test it
Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.
Frequently asked questions
How does the backreference work?
(\w+) captures the first word into group 1, and \1 later matches the identical text again, so only genuine repeats like 'the the' are found.
Will it flag 'that that' which can be correct?
Yes — 'that that' is sometimes grammatical. Treat matches as candidates for review, not automatic errors, to avoid false corrections.
Why use multiline and global flags for text cleanup?
Multiline makes ^ and $ match at each line boundary, and global applies the pattern across the whole text. Together they let you trim or collapse every line in one pass instead of looping manually.
How do I match across newlines?
The dot does not match newline by default. Use the s (dotAll) flag, or the [\s\S] character class, to span lines — essential for block comments and multi-line constructs.
Try the Regex Tester
Live regex matching with groups, flags and explanations.