Python (re)

Duplicate Word in PY

Detects consecutive duplicate words using a backreference.

Try it in the PY tester →

Pattern

regexPY
\b(\w+)\s+\1\b   (flags: gi)

Python (re) code

pyPython
import re

pattern = re.compile(r"\b(\w+)\s+\1\b", re.IGNORECASE)
input_text = "the the cat sat on the the mat"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

`\b(\w+)\b` captures a word. `\s+\1\b` matches whitespace followed by the same word (backreference `\1`). Case-insensitive.

Examples

Input

the the cat sat on the the mat

Matches

  • the the
  • the the

Input

no duplicates here

No match

Same pattern, other engines

← Back to Duplicate Word overview (all engines)