Sample:
>>> line = 'the the, To to'
>>> re.findall(r'\b(\w+) \1', line)
['the']
>>> re.findall(r'\b(\w+) \1', line, re.I)
['the', 'To']
>>> re.sub(r'\b(\w+) \1', r'\1', line, re.I)
'the, To to'
'the, To'
s/\v<(\w+) \1/\1/gi
s/\b(\w+) \1/$1/gi
-r 's/\b(\w+) \1/\1/gi'
3.4.3
Read the definition of re.sub
:
re.sub(pattern, repl, string, count=0, flags=0)
You are passing re.I
as count
(where it is allowing at most 2
replacements), not as flags
. Instead, try:
>>> re.sub(r'\b(\w+) \1', r'\1', s, flags=re.I)
# ^ note
'the, To'