Text Extraction
Duplicated Words Regex
Repeated words slip into writing all the time, especially across a line break where the eye does not notice them. This pattern captures a whole word in group 1, allows one or more whitespace characters, and then uses the backreference \1 to demand the exact same word again. Word boundaries on both sides stop it from flagging pairs like is island. The i flag makes the comparison case-insensitive, so This this is caught too. Run it with a global search over a document and every hit is a likely typo worth checking, although some phrases such as had had are grammatically correct.
Pattern (JavaScript)
/\b(\w+)\s+\1\b/iMatches a word boundary, then a group (one or more word characters), then one or more whitespace characters, then the same text captured by group 1, then a word boundary.
Examples
Matches
- ✓ the the cat
- ✓ This is is fine
- ✓ Hello hello there
Does not match
- ✗ the cat sat
- ✗ then the end
- ✗ is island
Try it
3 matches (JavaScript engine)
the the cat This is is fine Hello hello there the cat sat then the end is island
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\b(\w+)\s+\1\b/gi;
const text = "the the cat";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\b(\w+)\s+\1\b', re.I)
text = 'the the cat'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\b(\w+)\s+\1\b/i';
$text = 'the the cat';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\b(\\w+)\\s+\\1\\b", Pattern.CASE_INSENSITIVE);
String text = "the the cat";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
ℹ Go's RE2 engine has no backreferences. Match word pairs and compare group 1 with group 2 in code.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)\b(\w+)\s+(\w+)\b`)
text := "the the cat"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\b(\w+)\s+\1\b", RegexOptions.IgnoreCase);
var text = "the the cat";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
ℹ The Rust regex crate has no backreferences. Match word pairs and compare the two groups in code.
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"(?i)\b(\w+)\s+(\w+)\b").unwrap();
let text = "the the cat";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\b(\w+)\s+\1\b/i
text = 'the the cat'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I remove the duplicate and keep one copy?
- Replace each match with $1 in JavaScript. The replacement writes back the captured word once, so "the the" becomes "the".
- Why are there word boundaries around the pattern?
- Without them, "is island" would match because island starts with is. The boundaries force both copies to be complete words.
- Are all matches mistakes?
- Not always. English allows phrases such as "had had" and "that that" in some sentences. Treat matches as suggestions for review rather than automatic fixes.